How to run a command in a batch file that contains quotes ( "" )?
Asked Answered
P

3

6

I have a batch file which is attempting to run the following:

FOR /F "tokens=1" %%G IN ('git show --pretty="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a') DO echo %%G 

Which results in the git error ��fatal: Invalid object name 'format'.

However if I simply place the command itself in a batch file, I get output I expect.

git show --pretty="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a

Produces:

Files/MyFiles/header.html
Files/MyFiles/foo.html

The issue is something to do with the quotes around "format:".

I've tried escaping them using "", to no avail. I also tried ^. I've tried using the usebackq parameter.

This loop also works if you take out the --pretty="format:" argument, but then I get a bunch of extra text inserted.

Pitching answered 29/10, 2014 at 21:40 Comment(4)
Try using FOR /F "usebackq tokens=1... and then use backticks around your git command. The backticks are like the single quotes you currently have, but lean the other way.Leftover
You need to escape the = character in the command, e.g. --pretty^="format:". Otherwise I think it's OK.Keeter
To see this, I ran a test batch under a debugger (cdb cmd /c test.cmd) and set a breakpoint (bp kernel32!CreateProcessW) to inspect the command line that spawns the child (2nd x64 parameter: du @rdx).Keeter
I found the answer to be as @eryksun explained. Quoting the equal(=) sign resolved this problem.Rowdyish
R
1

You could redirect the output which should not cause a problem:

git show --pretty="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a >> out.tmp
FOR /F "tokens=1" %%G IN (out.tmp) DO echo %%G
del out.tmp

And that should work by redirecting the output to a file called out.tmp and then deleting it when you are finished with it.

Rhinitis answered 29/10, 2014 at 22:24 Comment(0)
W
0

The correct answer is in the comment by Eryk Sun: the = needs to be escaped. So the command should be:

FOR /F "tokens=1" %%G IN ('git show --pretty^="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a') DO echo %%G 

and that should work (only difference with original is ^= instead of =).

Wristwatch answered 25/5, 2023 at 9:25 Comment(0)
C
0

In PowerShell this should be very simple

foreach ($f in git show --pretty="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a) { $f }
Chaparajos answered 25/5, 2023 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.