devxlogo

Applying procedure to every file on a drive

Applying procedure to every file on a drive

Question:

For EVERY filename present on a hard drive, I need to convert the path and filename to a string and then evaluate the string for certain conditions. If those conditions are True, modify the string and append the string to “DataBase.txt. Otherwise, skip the file and move on to the next file, etc.

My program will successfully do this with one file at a time, but I don’t want to have to manually go through every file on the hard drive.How do I tell the program how to get every file on the drive? (Do first dir in C: then do and subdirs in that dir..ad nauseum…then do second dir in C; and so on.)

Thanks,
Rocky

Answer:

While a small ISAPI component would run faster, you can get away with a littlerecursive VBScript and the FileSystemObject. If you have a large number of files on thedrive, you may need to extend the script’s timeout. Here’s some code for doing that:

Set fs = CreateObject("Scripting.FileSystemObject")
Call ShowFolderList("c:")
Set fs=Nothing

Sub ShowFolderList(folderspec)
On error resume next
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
   ' Do any processing of the filename and path here
   ' For demonstration, this displays the filename and path
   ' If you want just the filename use f1.Name
Response.Write f1.Path & "
"
Next
Set fc2 = f.SubFolders
For Each f1 in fc2
   ShowFolderList(f1.Path)
Next
End Sub

devx-admin

Share the Post: