1

I am trying to embed the below command line into VBA:

for /f "skip=10 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.log ^2^>nul') do if exist "%%~fA" del "%%~fA

What I tried was:

Set objShell = CreateObject("WScript.Shell")
cmdLine = "cmd /k for /f ""skip=2 delims="" %%A in ('dir /a:-d /b /o:-d /t:c *.xls ^2^>nul') do if exist ""%%~fA"" del ""%%~fA"""
retVal = objShell.Run(cmdLine, 1, True)

It didn't work.

However, if I saved the code as .cmd, it worked.

12
  • Define "didn't work"? looks fine here. Commented Jun 7, 2016 at 18:18
  • Try using the Chr() function. Single quote is Chr(39). Commented Jun 7, 2016 at 18:19
  • Did you want there to be another quote at the end of "%%~fA?, your top example doesn't how it, but your bottom example does ""%%~fA"" Commented Jun 7, 2016 at 18:25
  • @Mat'sMug, the command didn't delete the files I intended to; also the retVal returns 1. Commented Jun 7, 2016 at 18:33
  • 1
    @JasonBrady for the other quotes, the syntax is correct. You can use Debug.Print cmdLine to check whether the string returned match your expectations. Commented Jun 7, 2016 at 18:37

1 Answer 1

1

Finally, I found the issue. It's not about the single quote.

You just need to change %%A into %A. However, if you save the code in .bat/.cmd, %%A works.

It is very finicky dealing with command line.

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

Comments

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.