VBScript to write all file and folder and subfolder names to a text file

Experts,

I am trying to create a text file listing all of the files, folders, and subfolders of a specific directory.  In the attached code snippet, I have been able to modify a code I found in another thread to do everything but go through all of my sub folders.

There are many more subfolders and files to be listed.

Any ideas??

Thanks in advance!!

Jeremy
const ForAppending=8
 
dim fso, fldShare, fldChild, fil, ts, sfolder, files, folder
 
set fso=createobject("Scripting.FileSystemObject")
sFolder = "C:\Users\Jeremy\Desktop\Halina\"
 
set fldShare=fso.getFolder("C:\Users\Jeremy\Desktop\Halina\")
Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)
Set folder = fso.GetFolder(sFolder)  
Set files = folder.Files 
 
'loop through all folders off the shared directory
for each fldChild in fldShare.subfolders
   
   NewFile.WriteLine("Directory " & fldChild.Name)  
 
   on error goto 0 'turn error handling off
   
   'now check files in the folder
 
   for each fil in fldChild.files
	NewFile.WriteLine("Directory " & fldChild.Name & "\" & fil.Name)
      on error resume next
 
      set ts=fil.openAsTextStream(ForAppending)
 
      if err.number<>0 then
         msgbox "(Read Only)"
      else
         ts.close
         set ts=nothing
      end if
 
      on error goto 0
 
   next
 
   set fil=nothing
 
next
 
'tidy up
set fldChild=nothing
set fldShare=nothing
set fso=nothing
 
msgbox "Done"

Open in new window

neelyjerAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
try:
Dim outputFile, searchDir
outputFile="C:\Users\Jeremy\Desktop\Halina\FileList.txt"
 
'make sure the folder name ends in a slash
searchDir="C:\Users\Jeremy\Desktop\Halina\" 
 
Set fso1 = CreateObject("Scripting.FilesystemObject")
Set NewFile = fso1.CreateTextFile(outputFile, True)
call ListDir(searchDir,"")
NewFile.close
Set NewFile=Nothing
Set fso1 = Nothing
 
'=======================================================
Sub ListDir(strDir, indent)
	NewFile.WriteLine( indent & "+" & strDir )
	indent = indent & "   "
	Set FSO = CreateObject("Scripting.FilesystemObject")
	Set pCurrentDir = FSO.GetFolder(strDir)
 
	For Each aItem In pCurrentDir.Files
		NewFile.WriteLine( indent & aItem.Name )
	Next
	For Each aItem In pCurrentDir.SubFolders
		aItem = strDir & aItem.Name & "\"
		Call	ListDir( aItem, indent)
	Next
	Set pCurrentDir = Nothing
	Set FSO = Nothing
End Sub

Open in new window

0
 
neelyjerAuthor Commented:
heilo.  That's perfect.  I modified it just a teensy bit.  The file names were indenting and started running across the page quite far.  So I removed the indent and added 3 spaces instead and removed the indent from the folders.   I also added a message box to tell me when the processing had completed.
Thanks SO much for your help.  The modified code is in the snippet.

Jeremy N.
Dim outputFile, searchDir
outputFile="C:\Users\Jeremy\Desktop\Halina\FileList.txt"
 
'make sure the folder name ends in a slash
searchDir="C:\Users\Jeremy\Desktop\Halina\" 
 
Set fso1 = CreateObject("Scripting.FilesystemObject")
Set NewFile = fso1.CreateTextFile(outputFile, True)
call ListDir(searchDir,"")
NewFile.close
Set NewFile=Nothing
Set fso1 = Nothing
 
'=======================================================
Sub ListDir(strDir, indent)
	NewFile.WriteLine( "+" & strDir )
	indent = indent & "   "
	Set FSO = CreateObject("Scripting.FilesystemObject")
	Set pCurrentDir = FSO.GetFolder(strDir)
 
	For Each aItem In pCurrentDir.Files
		NewFile.WriteLine( "   " & aItem.Name )
	Next
	For Each aItem In pCurrentDir.SubFolders
		aItem = strDir & aItem.Name & "\"
		Call	ListDir( aItem, indent)
	Next
	Set pCurrentDir = Nothing
	Set FSO = Nothing
 
End Sub
 
msgBox "Completed"

Open in new window

0
 
neelyjerAuthor Commented:
Absolutely fantastic!!!  Thanks for your help!!
0
 
hieloCommented:
>>The file names were indenting and started running across the page quite far.
OK. On what I posted, I added three spaces to the indentation every time the sub is called:
indent = indent & "   "

so you could have just changed that to one space OR to no spaces at all

>> I also added a message box to tell me when the processing had completed.
I was not sure if you were working from an ASP page (Response.Write "Finished" ) where msgbox would not have worked, but I knew you would add whatever you needed  :)
Take care
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.