Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vbscript to search for all files with an extension and save them to a CSV

Tags:

vbscript

I am trying to write a script that will search say C:\ and all of its sub folders for a specific extension and save all of theme to a CSV file. I have tried this but to no avail:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.GetExtensionName("*.txt")

Set colFiles = objFolder.Files

For Each objFile in colFiles
If objFile.Extension = "pfx" Then
    Wscript.Echo objFile.Name
    End If
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            Wscript.Echo objFile.Name
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next

Set WScript = CreateObject("WScript.Shell")

End Sub

I don't think I am going down the right path here. I am not proficient in the least in vb script it just happens to be the only thing I am allowed to use.

like image 848
Robert Avatar asked Sep 20 '13 15:09

Robert


2 Answers

Here you go:

Option Explicit 'force all variables to be declared

Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)

Recurse objFSO.GetFolder("C:\")
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "pfx" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub
like image 180
rory.ap Avatar answered Sep 30 '22 12:09

rory.ap


I know this is somewhat late, but that's the hard way to get that list. easiest is to sue the command line:

first identify the file where you want the directories like this:

set FileList="C:\path\path\file.csv"

Second identify the starting directory like this:

set Start="C:\path\path"

Then fill it like this:

for /F "usebackq" %i in (`dir /s/b "%Start%\*.pfx"`) do echo %~pi>>%filelist%

Done and done interactively.

PS - the post took out the backticks. That's the character usually under the tilde and they should enclose the complete directory command. Assuming a replace of ! for the backtick tick, the drectory command shold be !dir/s/b "%Start%\*.pfx"!

like image 25
Bill Avatar answered Sep 30 '22 11:09

Bill