Formatting date from bat file
-
I haven't had much experience working with batch files, so hopefully this will be a simple solution. I'm trying to get parse the output of the Windows
date
command from the mm/dd/yyyy format to the ddMMMyyyy format (i.e. 01/13/2010 to 13Jan2010). I'm attempting to do this with the following batch file:FOR /F "tokens=2,3,4 delims=/ " %%D IN ('date /T') DO (
IF %%D == 01 (
set month=JAN
) ELSE IF %%D == 02 (
set month=FEB
) ELSE IF %%D == 03 (
set month=MAR
) ELSE IF %%D == 04 (
set month=APR
) ELSE IF %%D == 05 (
set month=MAY
) ELSE IF %%D == 06 (
set month=JUN
) ELSE IF %%D == 07 (
set month=JUL
) ELSE IF %%D == 08 (
set month=AUG
) ELSE IF %%D == 09 (
set month=SEP
) ELSE IF %%D == 10 (
set month=OCT
) ELSE IF %%D == 11 (
set month=NOV
) ELSE IF %%D == 12 (
set month=DEC
)
echo %%E%month%%%F
)Usually this gives me an empty string for the %month% variable and I the last
echo
gives me 132010. However, any of the following changes can get it to work for a little while:IF %%D == "01" (
or
echo %%D
IF %%D == 01 (or
echo %%D
IF %%D == "01" (but I can re-run the bat file with these changes later, and then it gives me 132010 instead of 13Jan2010 again! I've tried this in XP, Vista, and Windows PE (where it will actually be used), and I get the same inconsistent results. What am I doing wrong? Thanks, Dybs
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
-
I haven't had much experience working with batch files, so hopefully this will be a simple solution. I'm trying to get parse the output of the Windows
date
command from the mm/dd/yyyy format to the ddMMMyyyy format (i.e. 01/13/2010 to 13Jan2010). I'm attempting to do this with the following batch file:FOR /F "tokens=2,3,4 delims=/ " %%D IN ('date /T') DO (
IF %%D == 01 (
set month=JAN
) ELSE IF %%D == 02 (
set month=FEB
) ELSE IF %%D == 03 (
set month=MAR
) ELSE IF %%D == 04 (
set month=APR
) ELSE IF %%D == 05 (
set month=MAY
) ELSE IF %%D == 06 (
set month=JUN
) ELSE IF %%D == 07 (
set month=JUL
) ELSE IF %%D == 08 (
set month=AUG
) ELSE IF %%D == 09 (
set month=SEP
) ELSE IF %%D == 10 (
set month=OCT
) ELSE IF %%D == 11 (
set month=NOV
) ELSE IF %%D == 12 (
set month=DEC
)
echo %%E%month%%%F
)Usually this gives me an empty string for the %month% variable and I the last
echo
gives me 132010. However, any of the following changes can get it to work for a little while:IF %%D == "01" (
or
echo %%D
IF %%D == 01 (or
echo %%D
IF %%D == "01" (but I can re-run the bat file with these changes later, and then it gives me 132010 instead of 13Jan2010 again! I've tried this in XP, Vista, and Windows PE (where it will actually be used), and I get the same inconsistent results. What am I doing wrong? Thanks, Dybs
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
It might be misinterpreted, as the
set
command gladly accepts a space as a last part of a variable-name. DOS might interpret the request like this;IF "%%D "=="01"(
How about something like the batch below?
set d=%Date:~3,2%
if "%d%" == "01" (
set month=JAN
)
else if "%d%" == "02" (
set month=FEB
)
..
echo %month%I are Troll :suss: