Wednesday, April 21, 2010

VBScript tips - Recursive file search write results to file

This is much like the previous example, except in this script instead of echoing out the file names, we are going to write the files (and their paths) to a results file.

strFolder = "C:\dell"

Set objFSO = CreateObject("Scripting.FileSystemObject")

GetFiles strFolder

Sub GetFiles(byval strDirectory)

Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile in objFolder.Files
'Here specify the output file location and name ex: "C:\Dell Files.txt"
WriteToFile "C:\Dell Files.txt",objFile.Path
Next
For Each objFolder in objFolder.SubFolders
GetFiles objFolder.Path
Next
End Sub

Sub WriteToFile(strFilePath, strValue)

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(strFilePath, ForAppending, True)
objTextFile.WriteLine strValue

End Sub

No comments: