Fix Excel VBA Shell Error 53 for MD and DIR Commands
Question details
Run a VBA macro that creates an output folder and saves a recursive directory listing to a text file.

- Product
- Microsoft Excel VBA
- Device & OS
- Windows
- Scenario
- The macro reads a folder path from cell C4 and writes the output of DIR /S to a text file in C:\Temp.
- Observed behavior
- VBA Shell returns run-time error 53 when it tries to run MD or DIR directly, although the DIR command works in Command Prompt.
Enter an existing folder path in cell C4 and make sure you can write to C:\Temp. Open the VBA editor with Alt+F11 and enable macros only if you trust the workbook.
Run DIR through the Windows command processor
Use the Windows command processor for DIR and redirection, and wait until the listing is complete.
MD and DIR are internal Command Prompt commands, not standalone executable files. VBA must launch the command processor to run them; the /c switch runs the command and then exits.
In Excel, press Alt+F11, then select Insert > Module.
Paste this code into the module: Sub ExportDirectoryListing() Dim folder As String, outputFolder As String, outputFile As String Dim command As String, exitCode As Long folder = Trim$(ActiveSheet.Range("C4").Value) outputFolder = "C:\Temp" outputFile = outputFolder & "\Dir.txt" If Dir$(outputFolder, vbDirectory) = "" Then MkDir outputFolder command = """" & Environ$("ComSpec") & """ /c dir """ & folder & """ /s > """ & outputFile & """" exitCode = CreateObject("WScript.Shell").Run(command, 0, True) If exitCode <> 0 Then MsgBox "DIR returned exit code " & exitCode End Sub
Return to Excel, select the sheet containing the path in C4, and run ExportDirectoryListing from Alt+F8. Open C:\Temp\Dir.txt after the macro finishes.

Use MkDir instead of the MD command
Use VBA's built-in MkDir statement when you only need to create a folder.
Review directory listings in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility, making it easy to move between spreadsheet tools. It does not fix Excel's VBA Shell error, but you can use WPS Spreadsheets to review exported listing data.

Frequently Asked Questions
Why does DIR work in Command Prompt but fail in VBA Shell?
DIR is built into the Windows command processor. Launch Environ$("ComSpec") with the /c switch to run DIR from VBA.
How do I handle folder paths that contain spaces?
Enclose the source folder and output file paths in double quotes when constructing the command string. The example macro quotes both paths.
How can VBA wait until the text file is ready?
Use WScript.Shell.Run with its third argument set to True, as in the example. VBA then waits for the command to finish before continuing.
Can I create nested folders with one MkDir statement?
No. MkDir creates one folder at a time, so create any missing parent folders first or use another folder-creation method.




