Stumped on how to parse directory tree with VBscript-VBForums
Results 1 to 7 of 7

Thread: Stumped on how to parse directory tree with VBscript

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Minneapolis
    Posts
    4

    Stumped on how to parse directory tree with VBscript

    Using the File and Folder objects I can search certain directories for files if I specify a path. Unfortunately I've come up on a situation where I need to search an entire directory tree. I'm not sure what kind of logic I will need for this because you never know how many levels a directory tree can have or maybe there is an object I can use that will do the dirty work for me, but I don't know.

    Can someone point me in the right direction?

  2. #2
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    you will need a recursive function!

    search on here, you'll find plenty

  3. #3
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Here is one i have put together using FSO, i am not quite clear if you want to scan all the directory in a given folder or just list one level.

    The following script will scan all folders and subfolders and list all file names.

    I have made another version which is more like explorer if you are after that then i will post it here.

    Hope it helps.

    Danial

    VB Code:
    1. <head>
    2. <title>Scan Folders</title>
    3. <link rel="stylesheet" href="styles.css" type="text/css">
    4.  
    5. </head>
    6. <%
    7. dim path
    8. dim done
    9. dim fileList()
    10. dim processing
    11. dim i
    12.  
    13.  
    14. path="C:\inetpub\"
    15.  
    16. ScanFolder path
    17.  
    18.  
    19. function ScanFolder(FolderPath)
    20. on error resume next
    21.  
    22.     dim fso, folder, file
    23.  
    24.     set fso = server.createobject("Scripting.FileSystemObject")
    25.     set folder=fso.getFolder(folderpath)
    26.  
    27.     if folderpath<>"" then
    28.         response.write "<h1>Path : " & folderpath & "</h1>"
    29.         response.write "<hr>"
    30.     end if
    31.    
    32.     i=1
    33.     For Each file In folder.Files
    34.         response.write i & ". " & folderpath  & file.name & "<br>"
    35.         i=i+1
    36.     next
    37.    
    38.     if folderpath<>"" then
    39.         response.write "<br>"
    40.     end if
    41.    
    42.     for each f in folder.SubFolders
    43.         scanfolder f
    44.     next
    45. End function
    46. %>
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Minneapolis
    Posts
    4
    Thanks for the response

    I tried adapting your code to run in the windows script host, I changed as little as I could to get it to run, but it will only return a list of files in the folder specified and not in all the subfolders, similiar to what i got before with my code.

    Im not sure if WSH uses the filesystem object differently or what the problem with the code is. Here is the code of yours that I changed.

    I will keep trying, but Im hoping you can see if there is a problem here



    Code:
    dim path
    dim done
    dim fileList()
    dim processing
    dim i
    const DESKTOP = "C:\Documents and Settings\wi006gr.PBI\Desktop\pdf"
    
    
    path=DESKTOP
    
    writetext(ScanFolder(path))
    
    
    function ScanFolder(FolderPath)
    on error resume next
    
    	dim fso, folder, file
    
    	set fso = wscript.createobject("Scripting.FileSystemObject")
    	set folder=fso.getFolder(folderpath)
    
    	if folderpath<>"" then
    		e = "Path : " & folderpath & vbnewline
    		
    	end if
    	
    	i=1
    	For Each file In folder.Files 
    		e = e & i & ". " & folderpath  & file.name & vbnewline
    		i=i+1
    
    	next
    	
    	if folderpath<>"" then
    		e = e & vbnewline
    	end if
    	
    	for each f in folder.SubFolders
    		scanfolder f
    	next
    scanfolder = e
    End function
    
    Function WriteText(vTextWrite)
    Dim oFs, oTextstream, vText, vFstatus 
    set oFs = Wscript.createobject("scripting.filesystemobject")
    
    	If oFs.FileExists("1.txt") = false then
    	  oFs.CreateTextFile ("1.txt")
    	end if
    
    set oTextstream = oFs.OpenTextFile("1.txt", 2)
    	 oTextstream.write(vTextwrite)
    	 oTextstream.close
    set oTextstream = Nothing
    
    End Function

  5. #5
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Hi,
    You have made few simple mistakes, first of all everytime you call writetext function it opens the file so that means it erases the content, secondly you are only outputing the name of the folders not the name of the files.

    Here is what i have managed to hack quickly, you can modify them and make it neater. The main theing is it works.

    Just copy and paste and save it as a .vbs file.

    Dont forget to specify your path name properly.

    Hope this helps

    Danial

    VB Code:
    1. dim path
    2. dim done
    3. dim fileList()
    4. dim processing
    5. dim i
    6.  
    7. Dim oFs, oTextstream, vText, vFstatus
    8. set oFs = Wscript.createobject("scripting.filesystemobject")
    9.  
    10. 'If oFs.FileExists("c:\1.txt") = false then
    11.   oFs.CreateTextFile ("c:\1.txt")
    12. 'end if
    13.  
    14. set oTextstream = oFs.OpenTextFile("c:\1.txt", 2)
    15.  
    16. path="C:\Documents and Settings\wi006gr.PBI\Desktop\pdf"
    17.  
    18. ScanFolder path
    19.  
    20. oTextstream.close
    21. set oTextstream = Nothing
    22.  
    23. function ScanFolder(FolderPath)
    24. 'on error resume next
    25.  
    26.     dim fso, folder, file
    27.  
    28.     set fso = wscript.createobject("Scripting.FileSystemObject")
    29.     set folder=fso.getFolder(folderpath)
    30.  
    31.     if folderpath<>"" then
    32.         writetext("Path : " & folderpath & vbcrlf)
    33.         writetext("__________________________________________" & vbcrlf)
    34.     end if
    35.    
    36.     i=1
    37.     For Each file In folder.Files
    38.         writetext(i & ". " & folderpath  & file.name & vbcrlf)
    39.         i=i+1
    40.     next
    41.    
    42.     if folderpath<>"" then
    43.         writetext(vbcrlf)
    44.     end if
    45.    
    46.     for each f in folder.SubFolders
    47.         scanfolder f
    48.     next
    49. End function
    50.  
    51. Function WriteText(vTextWrite)
    52.  
    53.      oTextstream.write(vTextwrite)
    54.  
    55. End Function
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Minneapolis
    Posts
    4
    Thanks bigtime for the help, the coding is a lot simpler than I thought it would be, but I just wasn't getting it right.

    Thanks again

  7. #7
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by ultraplop
    Thanks bigtime for the help, the coding is a lot simpler than I thought it would be, but I just wasn't getting it right.

    Thanks again
    No problem.

    Yes when dealing with recursive function, if you get it right then its really easy, or its really a pain !!

    Even though a lot of people hate FSO, it makes your life a hell of a lot easier !!

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.