smartyollie
smartyollie

Reputation: 181

Setting a CMD variable with multiple escapes

I want to set a Windows CMD variables to a UNIX date call, specifically:

date '+%H:%M:%S %m/%d/%y'

It works fine in my cmd window:

set STRING=^`date ^'^+^%H:^%M:^%S ^%m/^%d/^%y^'^`

echo %STRING%
date '+%H:%M:%S %m/%d/%y'

But when I put thin a batch file like try.bat

SETLOCAL EnableDelayedExpansion
set STRING=^`date ^'^+^%H:^%M:^%S ^%m/^%d/^%y^'^`
echo STRING is %STRING%

it comes out weird (whether I use delayed expansion or not):

>try.bat
STRING is `date '+m/y'`

Upvotes: 0

Views: 55

Answers (1)

SomethingDark
SomethingDark

Reputation: 14325

Escape % signs with more % signs. Also, ' don't need to be escaped.

set string=date '+%%H:%%M:%%S %%m/%%d/%%y'

Upvotes: 2

Related Questions