?
Solved

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

Posted on 2008-11-14
4
Medium Priority
?
1,987 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:neelyjer
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22966234
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
 

Author Comment

by:neelyjer
ID: 22966281
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
 

Author Closing Comment

by:neelyjer
ID: 31517027
Absolutely fantastic!!!  Thanks for your help!!
0
 
LVL 82

Expert Comment

by:hielo
ID: 22968097
>>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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question