-1

I am using command prompt in windows 7.
I found this strange that variable value modified inside if clause is updated only after if clause has been completely executed.
Here is a simple example to show this.
@echo off
set /a somevar=0
set match_string="match"
:loop
echo at loop beginning - somevar is %somevar%
if %match_string% equ "match" (
set /a somevar+=1
echo somevar inside if statement after increment - %somevar%
)
echo somevar after if statement is %somevar%
pause
goto loop

How can I have updated somevar inside if statement ?

Thanks.

user1371666
  • 445
  • 1
  • 5
  • 18
  • To downvoter of this question - can you make que score as zero because i tried to explain my question and wrote a sample code to verify . i can delete the question but experts who have replied will be at loss . also , it can help someone in future . thanks . – user1371666 Apr 10 '21 at 15:02

1 Answers1

1

You are suffering from a lack of delayedexpansion

@echo off
Setlocal EnableDelayedExpansion
set somevar=0
set "match_string=match"
:loop
echo before loop: %somevar%
if "%match_string%" == "match" (
     set /a somevar+=1
     echo inside loop: !somevar!
)
echo after loop: %somevar%
pause>nul
goto loop

Note !somevar! in the loop instead of %somevar%

You can read more on it by running set /? and setlocal /? from cmd

The reason however why I have not voted to close as duplicate is to also highlight your incorrect if statements and setting of variables. See the changes I made in the answer.

I suppose it is also good to mention that in this particular case, you do not require the parenthesized block, and therefore not need delayedexpansion either.

@echo off
set somevar=0
set "match_string=match"
:loop
echo before loop: %somevar%
if "%match_string%" == "match" set /a somevar+=1
echo after if statement: %somevar%
pause>nul
goto loop
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • my question was intended to update variable inside if statement and use that updated value inside same if statement . so i posted an example in which if has multiple statement to execute . delayedexpansion looks useful for that . But i think you misunderstood when you wrote 'you do not require the parenthesized block, and therefore not need delayedexpansion' – user1371666 Apr 10 '21 at 10:12
  • Yes. And I answered your question exactly as requested. The second part was just an FYI. Use it, Don't use it. – Gerhard Apr 10 '21 at 10:20
  • ok . thanks . btw , i tried one thing which worked for my case . i added 'if "%match_string%" == "match" set /a somevar+=1' before multiline if to use updated value. – user1371666 Apr 10 '21 at 10:49
  • So now you have 2 `if` statements? For the same case? That is a little inefficient is it not? You have numerous options to implement, so if you are not keen on using `delayedexpansion` then use `call` to the actions block and then you can add as many options as you like without having to use `delayedexpansion` – Gerhard Apr 10 '21 at 11:16
  • i will use delayedexpansion . Out of curiosity, can you give link for alternative 'call' method ? – user1371666 Apr 10 '21 at 14:53
  • I will edit my answer and show you. Just give me some time as I am on my phone and not at home. – Gerhard Apr 10 '21 at 14:55
  • ok . no problem , i will wait . – user1371666 Apr 10 '21 at 14:57