Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I traverse a directory tree 1

Status
Not open for further replies.

JoePytel

Programmer
Apr 15, 2002
6
0
0
US
I need to write A script that traverses a directory tree, displaying all filenames (and folder names) found within the tree.

Can Anyone help me?

Thanks
 
If interested this method uses a recursive algorithm to list all the folders, sub folders and the files of any directory passed to it. For this sample I have the start path hard coded in the Buttons onClick event but you could set it up to get the start path from a TextBox or what ever your preference.

cut and paste the code below into an new html page to test
NOTE: you may want to format the printout to make it look nicer.

<HTML>
<HEAD>
</HEAD>
<BODY >
<FORM NAME=&quot;F1&quot;>
<INPUT TYPE=&quot;button&quot; NAME=&quot;B1&quot; VALUE=&quot;BUTON 1&quot; onClick=&quot;TraverseTree('C:/inetpub/</Form>
</BODY>
</HTML>

<SCRIPT language = &quot;VBScript&quot;>
Dim FSO
function TraverseTree(fldr)
Dim fc
Dim fo
Dim fls
Dim F_item
Set FSO = createobject(&quot;scripting.filesystemobject&quot;)
Set fo = FSO.GetFolder(fldr)
Set fc = fo.SubFolders
Set fls = fo.Files
For Each F_item In fls
document.write F_item
document.write &quot;<p>&quot;
Next
For Each F_item In fc
TraverseTree(fldr + &quot;\&quot; + F_item.Name)
Next
end function
</SCRIPT>
 
I just want to be able to run command line so I stripped off all of the HTML and it won't run, nothing happens??

Any help?
 
I think kevinclark deserves a star for his help

A.C.
 
if i had a parent folder with 20 subfolder and each subfolder had 20 other sub folders and eac sub-sub-folder had another 20 folders, is it possible to loop through all those hundreds of folders and return the files for all as a whole?
 
I am not quite sure what you mean by returning the files as a &quot;whole&quot; but the algorithm would continue to loop (including writing the contents out to file or screen - which ever method you implement) until it either reaches the last level or runs out of stack space / memory.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top