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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.