Step 3SET Command - Mr.Math (2/4)
@ECHO OFFSET Test=1ECHO %Test%SET Test=%Test%-1ECHO %Test%PAUSE
Aww, what happened? That should have worked! Wait a minute... what was that about SET commands only making strings?
Yes. The SET command by itself will only create a string variable, meaning it will create the most literal interpretation of the value.
It doesn't think of "1 - 1" equaling "0", it thinks of "1 - 1" equaling "1 - 1."
So how do we change that?
We want the variable not to be directly copied, but Evaluated. This means we want to turn it from a string into an expression! A very easy change, simply add a /a to the SET command.
Here's a simple batch file to see it in action, or you can just type it into the Command Prompt manual. I suggest the latter; it's much faster, and the code isn't too complicated, but if you really want the batch file, here it is:
@ECHO OFFSET /a Test=2+2ECHO %Test%PAUSE
Fantastic! It expressed it perfectly. Now we know what changes need to be made to our 'math test.bat'
@ECHO OFFSET /a Test=1ECHO %Test%SET /a Test=%Test%-1ECHO %Test%PAUSE
Ok. So let's run it!
Great! It worked perfectly! But what next?
| « Previous Step | Download PDFView All Steps | Next Step » |



















































I noticed in this particular batch while using the set command like so..
set /a test=%test%-1
it can be written as
set /a test=test-1
with the same end result. Is this bad practice? Will it cause problems in longer strings? it just made sense logically to me not to include the percent.
set test=yourname
echo This is a test showing your name: %test%
If it didn't know the difference, it wouldn't display your name for %test% or it would put your name in both places.