top of page

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


Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

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

bottom of page