2

The variables _dd and _mm hold current day and month. 3 must be added to current day and if the day is greater than 27, 1 must be added to month (to change it to a month ahead).

Set /A _dd=%_dd%+3
REM **Set /A _mm=%_mm%+1**
echo %_dd%
IF %_dd% LSS 10 Set _dd=0%_dd%
IF %_dd% GTR 27 (
 Set _dd=01
 Set /A _mm=%_mm%+1
 Set _mm=0%_mm%
 Echo !_mm%! )

Set /A _mm=%_mm%+1 is working when ran outside IF statement, but not in action of IF statement.

Why is SET /A command line not working as expected after IF statement?

Mofi
  • 46,139
  • 17
  • 80
  • 143

3 Answers3

0

Few issues here. You are trying to use delayed expansion without setlocal enableDelayedExpansion clause. With the given test data you'll never enter the last IF. You are setting value inside brackets block without proper usage of delayed variable expansion.It's better to have setlocal/endlocal blocks. CHeck this:

@echo off
:: to use variable expansion with ! you need delayed expansion
setlocal enableDelayedExpansion
Set /A _dd=%_dd%+3
REM **Set /A _mm=%_mm%+1**
echo %_dd%
IF %_dd% LSS 10 Set _dd=0%_dd%
echo %_dd%


::set another value to _dd to match  the last if condition
set /a _dd=30
::::



IF %_dd% GTR 27 (

 Set _dd=01
 Set /A _mm=_mm+1
 Set _mm=0!_mm!
 Echo !_mm%! 

)
endlocal
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

The problem, (as it appears you have almost realised), is that variable expansion requires delaying in the If block.

Set /A _dd +=3
:: Set /A _mm +=1
Echo %_dd%
If %_dd% Lss 10 Set "_dd=0%_dd%"
IF %_dd% Gtr 27 (
   Set "_dd=01"
   Set /A _mm +=1
  Set "_mm=0!_mm!"
  Echo !_mm! )

Prior to using delayed expansion, you need to ensure that it has been enabled using:

SetLocal EnableDelayedExpansion
Compo
  • 36,585
  • 5
  • 27
  • 39
0

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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143