0

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

Ghoul Fool
  • 4,667
  • 9
  • 54
  • 100
  • 1
    there is a [more reiliable way](https://stackoverflow.com/a/18024049/2152082) to get the date in `YYYYMMDD` format – Stephan Jul 22 '18 at 12:12
  • 1
    As an addition to the answer provided, _(and the comment above)_, the usual recommendation is to use a method of determining the date, which is consistent machines and locales/regions. The example in [this answer](https://stackoverflow.com/a/51397310) does so using `RoboCopy` instead of `WMIC`. – Compo Jul 22 '18 at 12:18
  • 1
    Or use `for /f %A in ('powershell -nop -c Get-Date -f yyyyMMdd') do set USDate=%A` in a batch double the `%A` -> `%%A` –  Jul 22 '18 at 12:19
  • `SET %MM%=%MM%` is wrong syntax, it must read `SET MM=%MM%`, and I actually think it should be `SET MM=0%MM%`. To echo the result in the same code block you'd need [delayed expansion](http://ss64.com/nt/delayedexpansion.html). – aschipfl Jul 22 '18 at 12:53
  • You should include the code you are using to get the date. As was commented above there are a couple of ways to get the date in the specific format you need so that you will not need any extra code to reformat the date in the format you need. – Squashman Jul 22 '18 at 14:43
  • Possible duplicate of [How do I get current datetime on the Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format) – Squashman Jul 22 '18 at 15:54
  • One more thing to note is your use of the double colon got comments. Using them inside a parenthesised code block will cause unpredictable output. – Squashman Jul 22 '18 at 18:11

1 Answers1

3

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)

Stephan
  • 47,723
  • 10
  • 50
  • 81