By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Help making a CMD and TCC compatible batch file

I'm working with a CMD batch file that uses the satatement:

FOR /F "tokens=1-2" %%i IN ('"git show -s --format=%%%ci HEAD 2> NUL"') do (
   set REV3=%%i%%j

It works fine in CMD, but in TCC, Rev3 is not set (or set to nothing).

I haven't been able to find any way to write this so that it works in TCC - I think it has to do with the %'s - but I'd like to find a way that is fully compatible with CMD and TCC.

Any ideas?
I should probably point out that at the command line:
git show -s --format=%%ci HEAD
2021-01-14 15:44:20 -0700

I know I could escape the % and that works in CMD and TCC, a al:
git show -s --format=^%ci HEAD

And that works in CMD, but still nothing in TCC.
If it works in CMD and not in TCC - you might want to look at this:

TCC Compatibility with CMD

There are two options you should set if you regularly run batch files created for CMD:

OPTION / Startup / Duplicate CMD.EXE bugs (This is the default, and tells TCC to duplicate two bugs in CMD's IF command parsing.)

OPTION / Startup / CMD.EXE delayed expansion (If you have this startup option set for your CMD environment.)
I've now read the compatibility notes - tried setting "CMDVariables=yes" and didn't see a difference.

More research seems to show that the quotes are causing some problems:

FOR /F "tokens=1-2" %%i IN ('dir') do ( echo %%i %%j )

Works fine in both TCC and CMD, but:

FOR /F "tokens=1-2" %%i IN ('"dir"') do ( echo %%i %%j )

Works in CMD, but TCC gives:

TCC: Unknown command "dir"
Without the quotes, CMD messes up the escaping of the program parameter that requires "%ci" and the redirect doesn't seem to work either way without the quotes.

These scripts are in the bowels of some build systems, and I'm typically the only one that uses TCC, so my solution for now is to set ComSpec to cmd.exe and forget about it. But it would sure be nice not to have to do that.
You can split the command and for loop into separate statements.
git show -s --format=%%ci HEAD 2> NUL >temp.txt
for /f "tokens=1-2" %%i in (temp.txt) do set REV3=%%i%%j

Similar threads