3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Advanced Batch

Step 3SET Command - Mr.Math (2/4)

SET Command - Mr.Math (2/4)
Can it do math? Well let's make a simple batch to test it out.

@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 StepDownload PDFView All StepsNext Step »
4 comments
Jun 24, 2008. 5:52 PMTalkShowOnMute says:
I love your batch tutorials they have helped me to understand batching wonderfully.

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.
Apr 10, 2010. 8:43 PMGuard13007 says:
 It's not bad bad, but it's kinda bad, and someday, they may change the specification enough to cause your files to fail.
Apr 16, 2009. 7:37 PMNetworkNinja says:
I tried a longer code w/o using % signs, it didn't work. Is it really so much extra work to type in a couple extra letters?
Apr 10, 2010. 8:44 PMGuard13007 says:
 % signs are completely required, this is why your code broke.
Jul 17, 2009. 1:12 AMDNR says:
why do we put the % sign and wat does echo %test% mean
Apr 10, 2010. 8:44 PMGuard13007 says:
 echo is a command to print text on screen. The two percent signs signify the beginning and the end of a variable.
Aug 16, 2009. 3:28 PMkroq-gar78 says:
echo %test% means print out the value of test to the console, and I think the % signs are kinda like quotations for variables when referring to them.
Apr 10, 2010. 8:47 PMGuard13007 says:
The two percent signs signify the beginning and the end of a variable. That way it doesn't get confused with text and variables. Example:

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.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
22
Followers
5
Author:Neodudeman