Reputation: 6967
I'm trying to zero pad the month in a Windows 7 batch file that uses the date, specifically the month. I haven't been able to work out joining "0" to 7 successfully. Any ideas?
ECHO off
SETLOCAL
FOR /f %%I in ('wmic os get localdatetime /FORMAT:list ^| FIND "="') do SET "%%I"
SET "YYYY=%localdatetime:~0,4%"
SET /a "MM=1%localdatetime:~4,2% - 100"
SET "DD=%localdatetime:~6,2%"
FOR /f "tokens=%MM%" %%I in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do SET "month=%%I"
IF %MM% LSS 10 (ECHO %MM%
:: SET "%MM%=0%MM%" no dice
SET %MM%=%MM%
ECHO %MM%)
:: KLUDGE FOR TIMES UP TILL OCTOBER!
:: SET USdate=%YYYY%0%MM%%DD%
SET USdate=%YYYY%%MM%%DD%
:: US FORMAT 20180720
ECHO %USdate%
I can kludge the string, but come October, it won't work :) I've included the date, which I got from here
Upvotes: 0
Views: 301
Reputation: 56208
You've got a delayed expansion problem.
And another problem: numbers starting with zero are treated as octal (08
and 09
are not defined).
You can overcome both problems with a different approach (no if
needed at all):
set /a mm+=100
set mm=%mm:~-2%
where the first line adds 100
to the month (8
becomes 108
) and the second line extracts the last two characters (108
becomes 08
). And for December : 12
becomes 112
and 112
becomes 12
(yes, that's redundant, but it makes things much easier to handle all months the same way)
Upvotes: 3