473,419 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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

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
0 2147

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

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
1
by: Gema Gema | last post by:
I have a large collection of directories full of various files and am looking to create custom text files for the contents of each directory. Here is the situation: The directories are named...
20
by: Gary Manigian | last post by:
I have 2 tables, one-to-many, that contain bills of material(BOMs): tblBOM: lngBOMID (PK) strAssemblyPartNo strDescription tblBOMDetail: lngBOMDetailID (PK) lngBOMID (FK)
7
by: Les Coover | last post by:
I have struggled and struggled with this and still can not get it. The endless loop is supposed to be that way so that strings can be entered over and over again. When finished CTRL + C returns...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
2
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
9
by: reachmsn | last post by:
Hi, At the url http://www.python.org/doc/essays/graphs.html there is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.