Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"For" with "usebackq" not using back quotes

I have a batch file which was working quite happily, until the last couple of runs, and now it's not. The offending code is as follows:

set uncommittedchanges=1
for /f "tokens=* usebackq" %%a in (`"C:\Program Files\Git\cmd\git" -C "\my\git\repository" status`) do (
    if "%%a" == "nothing to commit, working directory clean" (
        set uncommittedchanges=0
    )
)

And the error I am getting is

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I'm sure I haven't made any changes to these lines since it was last working, and I can't see anything wrong with the code as it stands.

Can anyone spot what's wrong, or suggest a setting I may have inadvertently changed that affects usebackq?

like image 604
BeanFrog Avatar asked Jun 23 '16 14:06

BeanFrog


People also ask

What is %% f in batch script?

By default, /F breaks up the command output at each blank space, and any blank lines are skipped.

What is Delims?

The "delims=" option means do not parse into tokens (preserve each entire line). So each line is iteratively loaded into the %%i variable.

What are tokens in CMD?

The cmd token records the list of arguments and the list of environment variables that are associated with a command. The cmd token contains the following fields: A token ID that identifies this token as a cmd token. A count of the command's arguments. The argument list.


1 Answers

It's a bug of the invoking the child cmd.exe instance.
You need to use a workaround to avoid that the first token uses unescaped spaces.

The simplest way is to use CALL as it moves your program-token to the second place, and there it works without problems.

for /f "tokens=* usebackq" %%a in (`CALL "C:\Program Files\Git\cmd\git" -C "\my\git\repository" status`) do (

Windows start command not able to execute batch file

like image 119
jeb Avatar answered Sep 30 '22 15:09

jeb