Visual basic code to get list of files on C drive

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

Contact Me With Your Litigation Support Questions:

  • Twitter Long Shadow

© 2015 by Sean O'Shea . Proudly created with