Visual basic code to get list of files on C drive

Visual basic code to get list of files on C drive

April 17, 2017

You can use this nifty VBA code posted to OZ grid, to a get list of all files on your  C drive with any two extensions that you specify. 

 

I think it's apparent where to edit the file extensions on the lines which in this example begin, 'pdfFiles' and 'txtFiles'.    After you run the code a command prompt window will open and run for a while. 

 

You'll get long lists of the file paths for PDFs in column A and text files in column B.    

 

 

I tested it tonight with jpg and gif extensions and it worked just fine. 

 

 

 

 

Sub SO()
    
    Dim pdfFiles, txtFiles
    Const Q As String = """"
    
    With CreateObject("WScript.Shell")
        pdfFiles = Filter(Split(.Exec("CMD /C DIR " & Q & IIf(Right([B1], 1) = "\", [B1], [B1] & "\") & "*.pdf" & Q & " /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
        txtFiles = Filter(Split(.Exec("CMD /C DIR " & Q & IIf(Right([B1], 1) = "\", [B1], [B1] & "\") & "*.txt" & Q & " /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
    End With
    
    Range("A2").Resize(UBound(pdfFiles) + 1, 1) = WorksheetFunction.Transpose(pdfFiles)
    Range("B2").Resize(UBound(txtFiles) + 1, 1) = WorksheetFunction.Transpose(txtFiles)
    
End Sub

Please reload

Contact Me With Your Litigation Support Questions:

seankevinoshea@hotmail.com

  • Twitter Long Shadow

© 2015 by Sean O'Shea . Proudly created with Wix.com