Search code examples
windowsbatch-filecmd

CMD command not running in batch file


I am trying to create a batch file that runs a command.

The first part of the command checks to see if a particularly named window is open on my desktop. If it is it then closes that window (by sending a command to the program with the window open - Virtual Audio Cable).

@For /f "Delims=:" %A in ('tasklist /nh /v /fi "WINDOWTITLE eq Headphones"') do @if %A==INFO (echo Prog not running) else (start /min "audiorepeater_ks" "c:program files\Virtual Audio Cable\audiorepeater_ks.exe" /CloseInstance: Headphones)

Now this command seems to work fine when I execute it via the command line.

But it does nothing when I try to put it in a batch file

I have tried a .bat file and a .cmd file and also created a shortcut to the .cmd file where i have prepended the target field in properties with "C:\Windows\System32\cmd.exe /c"

Any ideas on how I can get this to run via batch?


Solution

  • You will have to escape "%A" with "%%A" as CristiFati pointed out. For more escape characters, please visit https://www.robvanderwoude.com/escapechars.php