0

I am using command prompt in windows 7 but my question should apply for any windows version xp and above.

I am using a variable but want to echo incremented value in an echo statement .

Currently , I increment then echo then decrement variable as shown below

set /a count=<some value>
set /a count+=1
echo %count%
set /a count-=1
Process count having <same value>

I hope there is a better way to increment the value of the variable by just using echo.

Thanks.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
user1371666
  • 445
  • 1
  • 5
  • 18

3 Answers3

3

You want something effectively like echo %count%+1? No, this won't work. The only way to do math in cmd is the set /a command.

I personally won't touch my original variable but use a dummy var;

set /a xcount=count+1
echo %xcount%

(just a thing of personal preference).

What actually would work:

set /a count=5
cmd /c set /a x=count+1
cmd /c set /a x=count+1 & echo/ this is plus one
cmd /c "<nul set /p "x=%Count% plus one is " & set /a x=count+1 &echo/. Ready."
echo %count% is still 5

because the set command is then done (in a new process) directly "on the command line" and set /a shows the result when done on the command line (but doesn't show the result in a batch file).

Contra: It's quite easy to show just the number (first cmd... line), but if you want to put some text "around", it quickly gets ugly (second line to append text, third line to put text around the number). And you open a new process. If you do it in a loop (as the variable name count suggests), it may slow down your script considerably.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • i am doing this is in a batch file . so I will try to use new dummy variable when i have to echo a value that is function of original variable. An interesting thing is that 'set /a count+=1 & echo %count%' also does not print updated value. – user1371666 Apr 10 '21 at 10:24
  • it does on the command line. It does not in a batch file. The trick I used is to force another instance of the command prompt to execute a command line. It's slow though (for a goto-loop of 1000 about 45 sec with `cmd /c` vs. about 2 sec with dummy-variable, but it's the closest solution to your literal question). – Stephan Apr 10 '21 at 11:45
  • Speaking about time: with the same test method your original approach is about the same as with the dummy-variable (about 2 sec), @Compo's solution is slightly behind with about 3 sec (`setlocal` and `endlocal` cost time, but less than I would have guessed). For the `set /a count+=1 & echo %count%` thing, read about [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Apr 10 '21 at 11:54
  • thanks . Now i understand delayed expansion . Should I delete this question to avoid -1 in my score ? – user1371666 Apr 10 '21 at 14:23
  • No. Do not delete it. You had an answer which solved your question. So you should mark it as the correct answer by selecting the grey tick mark on the left of the answer. This means you have accepted the answer as correct. Deleting a question with answers means people who have put time and effort into helping you are being affected. – Gerhard Apr 10 '21 at 14:45
  • ok . i will do that . setlocal ENableDelayedExpansion is useful and i will use that . thanks . – user1371666 Apr 10 '21 at 14:49
1

You could use delayed expansion, via SETLOCAL and ENDLOCAL, without creating an intermediate variable or running another instance of cmd.exe.

Rem Ensure extensions are enabled for SET /A functionality
Rem  and disable delayed expansion.
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define variable named count with the string value of an integer
Set "count=5"
Rem Print the value
Echo %count%
Rem Enable delayed expansion
SetLocal EnableDelayedExpansion
Rem increment the variable value
Set /A count += 1
Rem Print the new value
Echo !count!
Rem Discard variables defined or modified during previous SETLOCAL
EndLocal
Rem Print the value
Echo %count%
@Rem Optional PAUSE for GUI invoked script
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
0

A for /f loop is another means that avoids delayed expansion or setlocal / endlocal which can be used to get offset values without modifying the original count:

@Echo off
For %%O in (+ -)Do for /f "Delims=" %%G in ('Set /A "count%%O=1"')Do Echo(%Count% %%O 1 = %%G
T3RR0R
  • 2,747
  • 3
  • 10
  • 25