1

I want to use the below Command-line in VBA Code, the command is to get all .pdf and .docx files recursively and runs just fine in CMD

dir /s *.pdf *.docx

Now In VBA, have the below, which is printing out ALL files recursively and not the specified extensions

Sub Run()
    Debug.Print ShellRun("cmd.exe /c dir c:\ /s *.pdf *.docx")
End Sub

Public Function ShellRun(sCmd As String) As String
    Dim oShell As Object
    Set oShell = CreateObject("WScript.Shell")
    Dim oExec As Object
    Dim oOutput As Object
    Set oExec = oShell.Exec(sCmd)

    ShellRun = oExec.StdOut.ReadAll
End Function

1 Answer 1

2

Try this

Debug.Print ShellRun("cmd.exe /c dir /s c:\*.pdf c:\*.docx")

Edit: Following comment

You need to run dir /s PATH\*.pdf PATH\*.docx no space between \*. Also PATH should be repeated for both pdf and docx. However, in your code you had spaces and you also had the positions of c:\ and the flag /s swapped.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it's working fine now! Mind explaining to me how does this work briefly so i understand it better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.