1

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
3
  • 1
    easy, set /a num+=1 and please remove the spaces before and after = in a standard set statement. i.e set "num=1" see set /? from cmd Commented Mar 23, 2022 at 4:16
  • yeessss, thanks! I don't understand why to remove the spaces though. Commented Mar 23, 2022 at 4:20
  • let me post an answer. Commented Mar 23, 2022 at 4:29

1 Answer 1

3

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%
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.