kidl33t
kidl33t

Reputation: 1637

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

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.

Upvotes: 6

Views: 1197

Answers (3)

phuclv
phuclv

Reputation: 42002

In PowerShell this should be very simple

foreach ($f in git show --pretty="format:" --name-only 54173344ab18a7d8e9ff2614cca62b671c8c7e2a) { $f }

Upvotes: 0

Ken
Ken

Reputation: 94

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 =).

Upvotes: 0

Monacraft
Monacraft

Reputation: 6620

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.

Upvotes: 1

Related Questions