By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

Some code for you: Creating a recursive file listing, writing it out to a text file

P: 1
Amazing, I surfed around to find a simple utility that could list files recursively from a given top folder down through it, writing out the path and filename in simple \path\file_name form, without writing out empty folder names, if any. Well I found a couple utilities but they all tried to do all this other stuff, do it in Excel with no copy and paste, etc. Not a one that just writes a simple text file with the path and file name. So I finally surrendered to the idea that I would have to hack up a solution myself.

Do as follows:

1. In an Access .mdb file, create a new form.

2. Add these elements:

textbox:
Name: txtBasePath
Label: "Base Path:"

checkbox:
Name: chkIncludeBase
Label: "Include Base Path"

command button:
Name: cmdMakeList
Caption: "Make List"

Label (unassociated):
Name: whatever you feel like
Caption: "Look for list at C:\filelist.txt"

3. Save form.
4. Open form's code module
4a. Select Tools...References and make sure the 'Microsoft Scripting Runtime' library entry is checked.
5. Copy and paste the following into the form's code module:

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private cnt As Long
  4.  
  5. Private Sub cmdMakeList_Click()
  6.  
  7. On Error GoTo errorh
  8.  
  9. txtBasePath.SetFocus
  10. If IsNull(txtBasePath.Text) Then
  11.   Exit Sub
  12. End If
  13.  
  14. If txtBasePath.Text = "" Then
  15.   Exit Sub
  16. End If
  17.  
  18. Dim fso As New FileSystemObject
  19. Dim ts As TextStream
  20. Dim fldr As Scripting.Folder
  21. Dim basepath$
  22.  
  23. basepath = txtBasePath
  24. cmdMakeList.SetFocus
  25.  
  26. Set ts = fso.CreateTextFile("C:\filelist.txt", True, False)
  27. Set fldr = fso.GetFolder(basepath)
  28. writeFiles fldr, ts
  29.  
  30. ts.Close
  31.  
  32. MsgBox "Wrote " & cnt & " lines.", vbInformation, "Done"
  33. cnt = 0
  34. Exit Sub
  35.  
  36. errorh:
  37.  
  38. cnt = 0
  39.  
  40. MsgBox "Error occurred.", vbInformation, "Error occurred"
  41. MsgBox Err.Number & ": " & Err.Description
  42.  
  43. On Error Resume Next
  44. ts.Close
  45.  
  46. End Sub
  47.  
  48. Public Sub writeFiles(myfldr As Scripting.Folder, ts As TextStream)
  49.  
  50. Dim basepath$, name$
  51. Dim fil As Scripting.File
  52. Dim fils As Scripting.Files
  53. Dim fldr2 As Scripting.Folder
  54. Dim fldrs As Scripting.Folders
  55.  
  56. basepath = txtBasePath
  57.  
  58. Set fils = myfldr.Files
  59. Set fldrs = myfldr.SubFolders
  60.  
  61. For Each fil In fils
  62.  If fil.Attributes <> Directory Then
  63.   name = fil.path
  64.   If chkIncludeBase = 0 Then
  65.    name = Right(name, (Len(name) - Len(basepath)))
  66.   End If
  67.   ts.WriteLine name
  68.   cnt = cnt + 1
  69.  End If
  70. Next
  71.  
  72. For Each fldr2 In fldrs
  73.   writeFiles fldr2, ts
  74. Next
  75.  
  76. End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''
6. Save code.
7. Run form.

Find the folder from which you want to get the list, copy its path, add it to the textbox, then click the button. Voila, easy as cake.

If you find any boo-boos, please post. Otherwise you are free to use it, adapt it, add more error handling, add a folder-picker, whatever. The annoying part however is done for you. Enjoy!
Aug 22 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.