-
Mar 4th, 2002, 12:16 PM
#1
Thread Starter
New Member
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?
-
Mar 4th, 2002, 02:44 PM
#2
Frenzied Member
you will need a recursive function!
search on here, you'll find plenty
-
Mar 4th, 2002, 03:36 PM
#3
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:
<head>
<title>Scan Folders</title>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<%
dim path
dim done
dim fileList()
dim processing
dim i
path="C:\inetpub\"
ScanFolder path
function ScanFolder(FolderPath)
on error resume next
dim fso, folder, file
set fso = server.createobject("Scripting.FileSystemObject")
set folder=fso.getFolder(folderpath)
if folderpath<>"" then
response.write "<h1>Path : " & folderpath & "</h1>"
response.write "<hr>"
end if
i=1
For Each file In folder.Files
response.write i & ". " & folderpath & file.name & "<br>"
i=i+1
next
if folderpath<>"" then
response.write "<br>"
end if
for each f in folder.SubFolders
scanfolder f
next
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 :
-
Mar 4th, 2002, 07:55 PM
#4
Thread Starter
New Member
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
-
Mar 4th, 2002, 08:31 PM
#5
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:
dim path
dim done
dim fileList()
dim processing
dim i
Dim oFs, oTextstream, vText, vFstatus
set oFs = Wscript.createobject("scripting.filesystemobject")
'If oFs.FileExists("c:\1.txt") = false then
oFs.CreateTextFile ("c:\1.txt")
'end if
set oTextstream = oFs.OpenTextFile("c:\1.txt", 2)
path="C:\Documents and Settings\wi006gr.PBI\Desktop\pdf"
ScanFolder path
oTextstream.close
set oTextstream = Nothing
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
writetext("Path : " & folderpath & vbcrlf)
writetext("__________________________________________" & vbcrlf)
end if
i=1
For Each file In folder.Files
writetext(i & ". " & folderpath & file.name & vbcrlf)
i=i+1
next
if folderpath<>"" then
writetext(vbcrlf)
end if
for each f in folder.SubFolders
scanfolder f
next
End function
Function WriteText(vTextWrite)
oTextstream.write(vTextwrite)
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 :
-
Mar 5th, 2002, 12:58 PM
#6
Thread Starter
New Member
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
-
Mar 5th, 2002, 01:14 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|