Saving Contents of a Dir to a .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sgt D Pilla
    New Member
    • Jun 2007
    • 3

    Saving Contents of a Dir to a .txt file

    Hey, im only new, so please excuse me if i do something wrong.
    But im making a program where all the program does is write EVERY file and EVERY folder name into a .txt file called "KMOD! Log.txt"

    The program knows what directory to list, because the user of the program must navigate into the directory then press "Dump To Log File" to dump the contents into the log

    Currently the ONLY code i have is::
    Private Sub Command1_Click( )
    Open App.Path & "\log.txt" For Output As #1
    Do While Dir1.ListIndex < Dir1.ListCount - 1
    MsgBox.Dir1.Dir
    Dir1.ListIndex = Dir1.ListIndex + 1
    Loop
    Close #1
    End Sub
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Hi
    Sgt D Pilla
    Welcome to TSDN.

    You have reached the right place for knowledge shairing.

    Here you will find a vast resource of related topics and code.

    Feel free to post more doubts/questions in the forum.

    But before that give a try from your side and if possible try to post what/how you have approached to solve the problem.

    It will help Experts in the forum in solving/underestanding your problem in a better way.

    Please follow the posting guidelines in every new post/reply.

    please do not expect complete code from the experts of the forum. try from user side ,if u are struck up some where then ask for help.

    Comment

    • mailderemi
      New Member
      • Jun 2007
      • 14

      #3
      Originally posted by Sgt D Pilla
      Hey, im only new, so please excuse me if i do something wrong.
      But im making a program where all the program does is write EVERY file and EVERY folder name into a .txt file called "KMOD! Log.txt"

      The program knows what directory to list, because the user of the program must navigate into the directory then press "Dump To Log File" to dump the contents into the log

      Currently the ONLY code i have is::
      Private Sub Command1_Click( )
      Open App.Path & "\log.txt" For Output As #1
      Do While Dir1.ListIndex < Dir1.ListCount - 1
      MsgBox.Dir1.Dir
      Dir1.ListIndex = Dir1.ListIndex + 1
      Loop
      Close #1
      End Sub
      In this case you mean make a Search. this way is for begginers.
      It's easy to export the objects' names and types ;)
      1. Form:
      [CODE=vb]Option Explicit

      Private Type BrowseInfo
      hWndOwner As Long
      pIDLRoot As Long
      pszDisplayName As String
      lpszTitle As String
      ulflags As Long
      lpfnCallback As Long
      lParam As Long
      iImage As Long
      End Type

      Private Const BIF_RETURNONLYF SDIRS = 1
      Private Const MAX_PATH = 260
      Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
      Private Declare Function SHBrowseForFold er Lib "shell32" (lpbi As BrowseInfo) As Long
      Private Declare Function SHGetPathFromID List Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

      Public Function BrowseForFolder (ByVal hWndOwner As Long, ByVal sPrompt As String) As String
      Dim iNull As Integer
      Dim lpIDList As Long
      Dim lResult As Long
      Dim sPath As String
      Dim udtBI As BrowseInfo

      With udtBI
      .hWndOwner = hWndOwner
      .lpszTitle = sPrompt
      .ulflags = BIF_RETURNONLYF SDIRS
      End With

      lpIDList = SHBrowseForFold er(udtBI)
      If lpIDList Then
      sPath = String$(MAX_PAT H, 0)
      lResult = SHGetPathFromID List(lpIDList, sPath)
      Call CoTaskMemFree(l pIDList)
      iNull = InStr(sPath, vbNullChar)
      If iNull Then
      sPath = Left$(sPath, iNull - 1)
      End If
      End If

      BrowseForFolder = sPath


      End Function
      Private Sub cmdBrowse_Click ()
      Dim sPath As String

      txtPath.Text = ""
      sPath = BrowseForFolder (hWnd, "choose a directory")
      txtPath.Text = sPath
      End Sub

      Private Sub cmdDisplay_Clic k()

      'declare variables ...
      Dim sPath As String
      Dim clsDirs As Collection
      Dim udcTraverse As cTraverseDirs
      Dim lCounter As Long

      'set user difened class and collections...
      Set clsDirs = New Collection
      Set udcTraverse = New cTraverseDirs
      'get the path
      sPath = frmDirs.txtPath .Text
      If sPath = "" Then
      MsgBox "You must select a path"
      Exit Sub
      End If
      sPath = frmDirs.txtPath .Text & "\"
      'add it to collection ...
      clsDirs.Add sPath
      'initialize the counter ...
      lCounter = 1
      'traverse call ...
      Call udcTraverse.Tra verseDirectorie s(clsDirs, lCounter)
      End Sub
      [/CODE]
      and also it has a Class file:
      [CODE=vb]Public Sub TraverseDirecto ries(ByVal clsDir As Collection, ByVal vCollectionCoun ter As Variant)
      On Error GoTo td_error:

      Dim sDirName As String, sTempPath As String, sPathToTraverse As String


      sPathToTraverse = clsDir.Item(vCo llectionCounter )
      sDirName = Dir(sPathToTrav erse, vbDirectory)
      Do While sDirName <> "" ' Start the loop.
      ' Ignore the current directory and the encompassing directory.
      If sDirName <> "." And sDirName <> ".." Then
      ' Use bitwise comparison to make sure sDirName is a directory.
      If (GetAttr(sPathT oTraverse & sDirName) And vbDirectory) = vbDirectory Then
      ' Display entry for directory...
      sTempPath = sPathToTraverse & sDirName & "\"
      clsDir.Add sTempPath
      Else
      'create a collection of all files ...
      frmDirs.lstDisp lay.AddItem (sPathToTravers e)
      frmDirs.lstDisp lay.AddItem (sDirName)

      End If
      End If
      'Get next entry...
      sDirName = Dir
      Loop
      vCollectionCoun ter = vCollectionCoun ter + 1
      Call TraverseDirecto ries(clsDir, vCollectionCoun ter)
      Exit Sub
      td_error:
      'break out of the loop
      Exit Sub

      End Sub
      [/CODE]

      Comment

      • Sgt D Pilla
        New Member
        • Jun 2007
        • 3

        #4
        What does txtPath mean? and
        txtPath.txt ?

        is the txtPath a name of a controller ?

        Comment

        • Sgt D Pilla
          New Member
          • Jun 2007
          • 3

          #5
          Acutally guys, dont worry about it.
          I will do something else for my project. Its to difficult to get the program to write the name of every file in every folder in a certain directroy to a log file.

          Comment

          Working...