lovechillcool
lovechillcool

Reputation: 772

Embedding Variable in Command Line into VBA

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.

Upvotes: 1

Views: 169

Answers (1)

lovechillcool
lovechillcool

Reputation: 772

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.

Upvotes: 1

Related Questions