All occurrences of %_mm%
are replaced during preprocessing the entire command block starting with (
and ending with matching )
before the IF condition is executed at all by Windows command interpreter. This behavior can be seen by running the batch file from within a command prompt window without @echo off
at top to get output also the command lines and entire command blocks after preprocessing before execution.
It looks like delayed environment variable expansion as needed on accessing an environment variable within a command block being modified in same command block is not completely unknown as the line Echo !_mm%!
indicates although being syntactically wrong. Open a command prompt window, run set /?
and read the help output into the window carefully and completely. Delayed environment variable expansion is explained by help of command SET on an IF and a FOR example on which command blocks are most often used. The help of SET explains also arithmetic expression in detail.
Here is a solution not using delayed environment variable expansion by using a different process flow and which handles correct also month values 08
and 09
. See second comment block for details.
rem This integer increment is problematic in case of value is 08 or 09.
rem See the month increment below how to handle such values also correct.
set /A _dd+=3
echo %_dd%
if %_dd% LEQ 27 goto MakeTwoDigitDay
set "_dd=01"
rem 08 and 09 would be interpreted as invalid octal numbers. Therefore do a
rem string concatenation with 1 if the first character is a leading zero for
rem month to change month value from 01-09 to 101-109 to get the month value
rem with a leading 0 interpreted as decimal number and add 1. Otherwise add
rem 101 to months 1-12 with no leading 0. Then assign to month variable
rem just the last two digits of the month value being greater than 100.
if "%_mm:~0,1%" == "0" ( set /A "_mm=1%_mm% + 1" ) else ( set /A "_mm+=101" )
set "_mm=%_mm:~-2%"
echo %_mm%
:MakeTwoDigitDay
if "%_dd:~1%" == "" set "_dd=0%_dd%"
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
goto /?
if /?
rem /?
set /?