Home > Net >  How to add values to batch script variables?
How to add values to batch script variables?

Time:03-24

I know this may sound dumb, but I am a beginner at batch script, so I know almost nothing about it. Anyway, I want to change a variable; add 1 to it, here is my code:

@echo off
set num = 1
set num = %num%   1
echo %num%
@echo on

CodePudding user response:

Firstly, never use spaces in a standard set before and after =. This will create a variable with a trailing space and a value with a leading space. So technically you will have %num % and 1.

To demonstrate this, simply copy everything below and paste into a cmd prompt.

@echo off
set dummy = test
set dummy
echo %dummy%
echo %dummy %
@echo on

similarly with:

@echo off
set num = 1
set num | findstr /v "NUMBER"
set /a num =1
set num | findstr /v "NUMBER"
echo %num%
echo %num %
@echo on

will result in two variables, %num % with a value of 1 and %num% with a value of 1

The results from the above will clarify your question in the comments.

To solve your arithmetic question, simply use the /a switch to specify that the values are numerical and we can use arithmetic sequence operators etc. on it.

@echo off
set "num=1"
set /a num =1
echo %num%
  • Related