Wednesday, 16 March 2011

Back ticks equivalent in cmd / windows

How to get a result of one command and use it in another command in Windows shell, like with backticks in Unix/Linux shell?

Here is a working example:
for /f "tokens=1* delims=" %%s in ('gnuwin32\date +"%%Y%%m%%d_%%H%%M"') do echo output_%%s.log

> output_20110215_1057.log

Works in Windows XP, Vista, Windows 7.

1) The variable used in a for loop must be a one letter (at least in XP, Vista)
2) gnuwin32 is a set of GNU tools like grep/sed/awk/date which can be run natively in Windows without cost of additonal Cygwin-like layers