Not So Basic Batch Tutorial

 by Sejma
cmdbatchpic.bmp
In the previous tutorial we learned the core essentials of writing batch files.

If you have no clue what batch is, but want to learn it, refer to the "Very Basic Batch Tutorial".

In this tutorial you will learn more advanced commands and how to use them to create an application.
 
Remove these adsRemove these ads by Signing Up

Step 1: Variables Step (1/3)

Variables are things that are subject change.
When we create a variable, we are creating something that we want the computer to remember for us, something that we can use later on in a program;
we give the computer the value that we want to store and we give it a label to store it under.

we can create integers and strings using variables.

To create a variable you need to learn the SET command.

The SET command is what creates variables;

SET name=value

Type the following into your CMD:
SET name=hello

'name' is the name of the variable, and 'hello' is what the variable is storing

so now every time you type "echo name" it should say "hello" yes?
NO

if you want to display a variable you must put percentage (%) signs around it.

So therefore if you type "echo %test%" and it should say "hello" yes?
YES
1-40 of 42Next »
admin says: Jun 17, 2008. 12:19 PM
Hello, and welcome to the Instructables community! It's great that you've decided to tell the world about something you've made by publishing an Instructable. We just wanted to let you know that your project still needs a little more work if you want it to be well received on Instructables. Projects that don't include certain basic elements tend not to get the attention that they deserve, and so we'd love for you to check out the list below of what makes a successful Instructable. Successful projects on Instructables include: - clearly written details of a finished project with instruction - as many steps as are necessary to explain your project - clear images that you took of your project for most, if not all of your steps - an intro image - proper spelling and grammar - appropriate cautions or safety considerations I'll give you another opportunity to make any final changes to your project before we publish it. Once you're all set to go, please republish your project and send me a quick comment letting me know that you've made some changes. I'll give it a quick final check to make sure you're on the right path, and then remove this note. Thanks for your submission and we hope to see your project published soon!
Lance Mt. says: Feb 27, 2009. 10:25 PM
Hey man, I have a small problem. I'm tryign to make a .bat to begin a thing based of the %random% command. I'd put say.... 3485804 and if it goes to it, it begin a "Insert thing evil program here". I presumed this would work @echo off title Random Number Gen by seJma color 2f :top cls echo Your Number is... echo %random% if %1227% goto begin goto top :begin pause start www.1227.com
Lance Mt. in reply to Lance Mt.Feb 27, 2009. 10:57 PM
Nevermind
i used the if %random%==1227 goto begin
Lance Mt. in reply to Lance Mt.Feb 28, 2009. 12:27 AM
How do i limit the number? Is the a "equal to or less than" or just "less than"?
Prof. Pickle in reply to Lance Mt.Dec 30, 2012. 4:42 AM
Even though it's old...

SET /a Var=%RANDOM% %%max + min

That method uses modulo expressions to constrain the number.

Example:
If "max" had the value of 10, and "min" had the value of 1...
The variable "Var" would be a number between 1 and 10.
goosegg34 says: Sep 20, 2009. 8:26 AM
I have a question I want it to do a goto comand when you type in 1 how do you do that
so far I hav
@echo off
echo welcome to brian
echo brian will now add 4 1698 times
set/a num=4
:furt
set/a num=%num%+4
echo %num%
if %num%==6792 goto :end
goto :furt
:end
echo you have reached 6792
echo continue?
set v1=goto :count1
set v2=goto :g
echo press 1 to continue
echo press 2 to stop
set/a nu=6792
set/a n=0
set /p you=
if %you%==1 echo %v1%
if %you%==2 echo %v2%
Prof. Pickle in reply to goosegg34Aug 28, 2012. 11:55 PM
I fixed your code.

@echo off
echo welcome to brian
echo brian will now add 4 1698 times
set /a num=4
:furt
set /a num=%num%+4
echo %num%
if %num%==g792 goto end
goto furt
:end
echo You have reached 6792
echo continue?
echo press 1 to countinue
echo press 2 to stop
set /a nu=6792
set /a n=0
set /p you=
if %you%==1 goto count1
if %you%==2 goto g

Sejma (author) in reply to goosegg34Sep 20, 2009. 9:35 AM
is this the whole program? if so, then i don't understand where %v1% and %v2% are meant to goto. but if u want it to work, take out the "echo" statements in the last two lines:

if %you%==1 %v1%
if %you%==2 %v2%

edfel01 says: Nov 7, 2008. 3:59 AM
im very new to batch and i want to know everything. how do u open up .bat files on notepad and how do u create an account and the number thing wont work for me. plz help
Guard13007 in reply to edfel01Apr 10, 2010. 9:24 PM
 You can't open them in Notepad without "hacking" your registry, a much simpler idea is to install Notepad++ (Google it, don't feel like putting a link) and it's better cause Notepad++ has syntax highlighting.
Prof. Pickle in reply to Guard13007Aug 28, 2012. 11:48 PM
It's called Right-click, EDIT.
erikals says: Jul 14, 2009. 1:46 AM
Your random number generator is great, though one problem. It increments in 10s instead of generating random numbers. Do you think you can fix this? Otherwise, a really excellent Instructable.
bhjohnp1 in reply to erikalsNov 17, 2010. 2:44 PM
if you think about it, a random number generator is virtually impossible. it will have to follow some sort of pattern or otherwise create something that changes the pattern itself and even THAT is considered a pattern....
CapnTac says: Jul 26, 2009. 1:45 PM
Okay, I just learned how to use this for the most part, but I just have one question... How do you get a batch file to open and run on your computer???
Guard13007 in reply to CapnTacApr 10, 2010. 9:22 PM
 Double-click.........

Is this your first computer?
CapnTac in reply to Guard13007Apr 11, 2010. 6:19 AM
I don't remember making that comment, haha. Whoops.
will421 in reply to CapnTacJul 22, 2010. 6:56 AM
You fell asleep while your laptop was open, probably. My dad chatted on Skype while asleep one time. and it was not gibberish it was like, when he said he was just woke up on the 4th message, I was like, "He was asleep?". And I have sleep talked before.
kalimanni says: May 16, 2010. 7:51 PM
can i do the same thing so it will count letters and numbers such as for running through various combinations of words/ digits?
YummyPancakes says: Jul 12, 2008. 6:20 PM
I pressed 2, and it said "BYE!!!" instead of "BI!" What a rip-off.
microchip55 in reply to YummyPancakesNov 6, 2009. 12:19 PM
lol thats not a rip-off lol.
Sejma (author) in reply to YummyPancakesJul 13, 2008. 1:30 AM
haha, very observant......i'll change it just now
Deadkirby says: May 12, 2009. 12:05 PM
This is a bit wird but variables r easy
lobo_pal says: Feb 19, 2009. 8:15 PM
Agh, my brain. splat.
YummyPancakes in reply to lobo_palMar 25, 2009. 7:39 PM
Nah, more like plop. Unless you watch a lot of TV. Then it's ker-plunk (slurp). Sorry, been watching too many hulu commercials.
will421 in reply to YummyPancakesApr 27, 2009. 9:30 PM
Ha! Like your icon (It`s not an avatar rofl)
Lance Mt. says: Mar 6, 2009. 5:56 PM
Excuse me admin, This is a part 3 of 3, i don't think it needs much more but if you insist. -Just thinking, Chris
Lance Mt. says: Feb 11, 2009. 9:39 PM
Love it, nice man. Also using the "if %you%==1" command, made a game for my friends that if you get a question wrong you go to www.1227.com and if not, you continue...

-Cheers, Chris

Derin in reply to Lance Mt.Feb 12, 2009. 7:43 AM
what is 1227.com
Lance Mt. in reply to DerinFeb 15, 2009. 8:36 PM
Its a rickashtely community that has every rick rolol link imaginable, and you CAN view them too, its not advised
lobo_pal in reply to Lance Mt.Feb 19, 2009. 8:16 PM
Send them to tubgirl.com
Lance Mt. in reply to lobo_palFeb 21, 2009. 7:11 PM
Which is?????
lobo_pal in reply to Lance Mt.Feb 21, 2009. 8:23 PM
Go there, it has this really funny picture on the front page.
Arbitror says: Jun 17, 2008. 5:42 PM
Sorry, but NeoDudeMan beat you to this instructable! He has already made a basic, intermediate, and advanced instructables! Your is almost the same... I wonder if there is any copy'n'pasting involved...
Sejma (author) in reply to ArbitrorJun 17, 2008. 10:31 PM
yeah his tutorials are pretty good, but i'm not a "copy, paste" type of guy, I just wanted to explain batch through my eyes.
Arbitror in reply to SejmaJun 18, 2008. 4:21 PM
OK!
TATcreator in reply to ArbitrorNov 10, 2008. 5:23 PM
So you have already met Sejma, then? you acted as if he was new to you! P.S. Show us how you made the flash drive backup, please.
RyanPotter in reply to ArbitrorJun 17, 2008. 9:46 PM
I doubt he stole any ideas, because batch file tutorials can't really be made too uniquely.
QwertyuioLP says: Aug 11, 2008. 3:39 AM
Could you create a random number generator then?
Sejma (author) in reply to QwertyuioLPAug 11, 2008. 9:10 AM
yes, try this (you can take out the "cls" if you want) @echo off title Random Number Gen by seJma color 2f :top cls echo %random% pause goto top
QwertyuioLP in reply to SejmaAug 12, 2008. 3:37 AM
thnks
1-40 of 42Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!