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

Advanced Batch
«
  • cmd window vista.JPG
  • Run CMD.JPG
  • Accessories CMD.JPG
PLEASE DIRECT ANY QUESTIONS YOU MAY HAVE TO MY BLOG:
http://tamsanh.com/blog/2008/07/11/batch-tutorial-3-advanced-batch/

I do not visit instructables anymore, and you will get a faster response that way.

Hey Guys! Tam Nguyen here. It's been a long time coming, but it's here now! I present to you the next instalment of my Batch Tutorials!

You may (or may not) have read my other instructables:

Basics of Batch
Slightly More Advanded Batch

This Instructable borrows some elements from the first two Instructables. Namely:
ECHO Command
CD Command
DIR Command

If you have not read the other two, I suggest looking over those commands right-quick. I'll wait here.

Ok!

By the end of this Instructable, you will be intimate with:
START Command
SET Command
IF Command
GOTO Command
and
> and >> Parameters

With these commands, you will be able to make dynamic batch files which can change according to user input, and create and expand.

So let's open up that good 'ol Command Prompt and get started!

Run -> cmd

Alternatively, you can go to:

Start->Program Files->Accessories->Command Prompt

My explainations in this instructable got a little long, so I've split up some commands into more than one page mainly for aesthetic purposes; I liked seeing the pictures while reading the text without having to scroll up and down.

Rule of thumb for picture viewing; Usually after every Code Snippet, there is a picture.

Enjoy!
 
Remove these adsRemove these ads by Signing Up
 

Step 1START Command

START Command
The START Command is a nice little command which starts stuff!

It's a simple command, and will help us warm up before we get to the harder commands.

The parameters of the command are exactly what you think they are.
START ThingToBeStarted.exe
Pretty simple.
START WMPlayer.exe
Not only can you START .exe's, you can also type a website in, and it will open up in your default browser!
START www.Instructables.com
Pretty easy, eh? Ok. Now let's move on to the real meat of the Instructable: SET Command.
« Previous StepDownload PDFView All StepsNext Step »
148 comments
1-40 of 148next »
Apr 26, 2012. 1:46 AMBladesofvengence says:
I made a Number guessing game batch but i cant get it to work.
@ECHO OFF
title Guess what number i am thinking of!
color D0
if exist SAVEGAME.bat goto top
SET /a score1=10
SET /a score2=10
SET /a score3=10
SET /a score4=10
SET /a score5=10
SET name1=aaa
SET name2=bbb
SET name3=ccc
SET name4=ddd
SET name5=eee
Goto mum
:top
call SAVEGAME.bat
:mum
SET /a GuessLimit=100
:start
SET tries=11
cls
ECHO Guess what Number I'm thinking of. (0-100)
ECHO Be careful, you only have limited tries!
ECHO Press any key to continue.
pause>nul
SET /a GuessNum=0
:begin
SET /a Answer=%RANDOM%
if %Answer% gtr 100 goto begin
:Retry
cls
SET /a tries=%tries%-1
SET /a Left=11-%tries%
if %tries% equ 0 (goto penalty)
ECHO You have %tries% attempts left.
ECHO Please type your guess number then press enter.
SET /p Guess=
SET /a GuessNum=%GuessNum%+1
IF %Guess%==overlord goto END2
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO Press any key to continue.
pause>nul
ECHO.
GOTO Retry
:penalty
ECHO Sorry, you have run out of tries.
ECHO Better luck next time!
ECHO The Answer was %Answer%
ECHO Do you want to play again? Yes/No
SET /p Answer=
IF %Answer%==yes GOTO start
IF %Answer%==Yes GOTO start
IF %Answer%==no GOTO Exit
IF %Answer%==No GOTO Exit
:END
cls
ECHO You are Correct!
ECHO The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO Please write your name.
SET /p name=
IF %Left% LSS %score1% GOTO sub1
IF %Left% LSS %score2% GOTO Sub2
IF %Left% LSS %score3% GOTO Sub3
IF %Left% LSS %score4% GOTO Sub4
IF %Left% LSS %score5% GOTO Sub5
:overlord
ECHO Do you want to play again? Yes/No
SET /p Answer=
IF %Answer%==yes GOTO start
IF %Answer%==Yes GOTO start
IF %Answer%==no GOTO Exit
IF %Answer%==No GOTO Exit
:END2
cls
ECHO You are Correct!
ECHO The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO Do you want to play again? Yes/No
SET /p Answer=
IF %Answer%==yes GOTO start
IF %Answer%==Yes GOTO start
IF %Answer%==no GOTO Exit
IF %Answer%==No GOTO Exit
:Exit
exit
:sub1
SET /a score5=%score4%
SET /a score4=%score3%
SET /a score3=%score2%
SET /a score2=%score1%
SET /a score1=%Left%
SET name5=%name4%
SET name4=%name3%
SET name3=%name2%
SET name2=%name1%
SET name1=%name%
GOTO HiscoreList
:Sub2
SET /a score5=%score4%
SET /a score4=%score3%
SET /a score3=%score2%
SET /a score2=%Left%
SET name5=%name4%
SET name4=%name3%
SET name3=%name2%
SET name2=%name%
GOTO HiscoreList
:Sub3
SET /a score5=%score4%
SET /a score4=%score3%
SET /a score3=%Left%
SET name5=%name4%
SET name4=%name3%
SET name3=%name%
GOTO HiscoreList
:Sub4
SET /a score5=%score4%
SET /a score4=%Left%
SET name5=%name4%
SET name4=%name%
GOTO HiscoreList
:Sub5
SET /a score5=%Left%
SET name5=%name%
:HiscoreList
ECHO.
ECHO HISCORES
ECHO 1. %name1% %score1%
ECHO 2. %name2% %score2%
ECHO 3. %name3% %score3%
ECHO 4. %name4% %score4%
ECHO 5. %name5% %score5%
if exist SAVEGAME.bat del SAVEGAME.bat
ECHO @echo off>SAVEGAME.bat
ECHO set name1=%name1%>>SAVEGAME.bat
ECHO set name2=%name2%>>SAVEGAME.bat
ECHO set name3=%name3%>>SAVEGAME.bat
ECHO set name4=%name4%>>SAVEGAME.bat
ECHO set name5=%name5%>>SAVEGAME.bat
ECHO set score1=%score1%>>SAVEGAME.bat
ECHO set score2=%score2%>>SAVEGAME.bat
ECHO set score3=%score3%>>SAVEGAME.bat
ECHO set score4=%score4%>>SAVEGAME.bat
ECHO set score5=%score5%>>SAVEGAME.bat
pause>nul
GOTO overlord
May 18, 2010. 12:30 PMfhkoekje says:
i changed your code for the number guessing game
i added a simple cheat and made the game a little easier:
the number doesn't go higher than 100

@ECHO OFF
:start
cls
SET /a GuessNum=0
:begin
SET /a Answer=%RANDOM%
if %Answer% gtr 100 goto begin
:Retry
ECHO Guess what Number I'm thinking of.
SET /p Guess=
SET /a GuessNum=%GuessNum%+1
IF %Guess%==cheat goto END
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
pause>nul
cls
GOTO Retry
:END
cls
ECHO You are Correct!
echo The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO.
pause
goto start
Aug 18, 2011. 10:35 PMsilvan9145jr says:
I added on to yours.

@ECHO OFF
title Guess the Number!
color 0b
SET /a GuessLimit=15
:start
cls
ECHO Guess what Number I'm thinking of. (0-100)
SET /a GuessNum=0
:begin
SET /a Answer=%RANDOM%
if %Answer% gtr 100 goto begin
:Retry
SET /p Guess=
SET /a GuessNum=%GuessNum%+1
IF %Guess%==cheat goto END
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
IF %GuessNum% GTR %GuessLimit% (
start http://www.google.com/
)
ECHO.
GOTO Retry
:END
cls
ECHO You are Correct!
echo The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO.
echo Press any key to play again. :)
pause>nul
goto start

You can change the google.com to whatever you want so that if they dont guess in 15 tries they go to the designated website >:D
Jul 16, 2010. 11:57 AMteh_pwnage says:
@ECHO OFF color 29 :hmm? Echo Chose the right number or get PWNED! echo. echo. set /p lol=Choose or ELSE!!!))) if %lol%== 8 good... if not %lol%== 8 goto pwn :pwn start http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=related/ exit :good... echo You got lucky... pause >nul
Apr 21, 2011. 2:38 PMcrazyman135790 says:
cd..
cd..
cd..
cd..
cd..
dir /s
Feb 9, 2010. 6:23 PMgraboy says:
^^ I think I'm the only person who got the IF's AND's or NOT's thing. "NOR" does anyone other than us know what were talking about.
Aug 17, 2010. 7:22 AMFiles says:
:o I think I got it too. "IF" "AND" "NOT"
Feb 10, 2011. 1:22 PMwill421 says:
I GOT IT TOO!
IF you thought they were batch commands AND NOT just in regular old all caps, you got it!
Aug 17, 2010. 7:41 AMFiles says:
I mine as well should add my variation.I got ideas from various people, but I mainly just edited and added in some new echo's and added some extra info.



@ECHO OFF
title Command Prompt - Made by NetworkNinja Editted by Mikeyy
color 20
echo Calculator V1.3
echo Made by NetworkNinja echo Editted by Mikeyy
echo ---------------------
echo LEARN:
echo * = Multiply
echo / = Divide
echo + = Addition
echo - = Subtracting
echo ---------------------
:blah
ECHO.
SET /p UDefine=
SET /a UDefine=
%UDefine%
ECHO =
ECHO
%UDefine%
ECHO.
PAUSE
ECHO.
echo THANK YOU FOR USING OUR CALCULATOR!
ECHO -------------------------------------------------------------------------------- ECHO.
goto blah
Jul 16, 2010. 11:59 AMteh_pwnage says:
You could do this: @echo off color 0a :lol echo %random%%random%%random%%random%%random%%random%%random%%random%%random% goto lol
Jul 16, 2010. 11:55 AMteh_pwnage says:
@ECHO OFF color 29 :hmm? Echo Chose the right number or get PWNED! echo. echo. set /p lol=Choose or ELSE!!!))) if %lol%== 8 good... if not %lol%== 8 goto pwn :pwn start http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=related/ exit :good... echo You got lucky... pause >nul
Jul 10, 2008. 12:52 PMEvilthingamabober says:
@echo off
color 29
Echo Chose the right number or get PWNED!
SET /p doom1=
SET boom2=8
IF %doom1%NEQ%boom2% (
start http:/www.epicwinrar.com/
)
I tried this batch out but it didn't work. What did I do wrong?
Jul 16, 2010. 11:54 AMteh_pwnage says:
@ECHO OFF color 29 :hmm? Echo Chose the right number or get PWNED! echo. echo. set /p lol=Choose or ELSE!!!))) if %lol%== 8 good... if not %lol%== 8 goto pwn :pwn start http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=related/ exit :good... echo You got lucky... pause >nul
Jun 25, 2009. 9:14 AMwill421 says:
EKKKK!!! Rick roll!
Jul 16, 2010. 11:54 AMteh_pwnage says:
@ECHO OFF color 29 :hmm? Echo Chose the right number or get PWNED! echo. echo. set /p lol=Choose or ELSE!!!))) if %lol%== 8 good... if not %lol%== 8 goto pwn :pwn start http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=related/ exit :good... echo You got lucky... pause >nul
Jul 11, 2008. 5:40 PMEvilthingamabober says:
Thanks. Time to own my parents...
Jul 12, 2009. 10:16 AMtinkerC says:
How do you read a file using Batch?
Apr 10, 2010. 8:55 PMGuard13007 says:
 It's really complex and long, don't bother unless you really need to.

Unless you're just trying to read one line, in which case:

set var=<"file"
echo %var%

But that only works in XP...
Apr 11, 2010. 5:27 AMtinkerC says:
 No, I mean, read a file as in a configuration file.
Jun 26, 2010. 12:00 PMMacDynamo says:
::type Configure.Extension | find /i "Variable" > OutputFile.Extension ::set /p SetupVariable= < OutputFile.Extension ::set ProgramVariable=%SetupVariable:~X,Y% ::X and Y being the first and last character number eg. Variable=10 is x=10, y=11 ::PM me if you still need help (had to make this brief for some reason)
Jul 15, 2009. 8:21 PMscienceguy says:
HELP i want to create a line of code that will allow me to test my newest fork virus(s) and still allow me to regain control without having to reboot. It does not need to be its own program, just a line that i insert in the file. for example, i need a line that, when i press the "A" key in any of the fork windows, it will close all the fork windows, thereby not allowing it to duplicate anymore seen as it has stopped all instances running. so, more or less, an exit command for ALL DOS windows or all windows with a certain name. finally, if this is not fesable, a way to just stop it duplicating, without closing all the windows would work too. thanx
Apr 10, 2010. 8:53 PMGuard13007 says:
 Here's a way to do it.

set loop=0
:loop
(virus stuff)
set loop+=1
if "%loop%"=="13" goto end
goto loop
:end
pause

The line "set loop+=1" is a command to add 1 to the current value of the variable.
May 18, 2008. 8:36 AMelmynase says:
can you divide?
Apr 16, 2009. 6:00 PMNetworkNinja says:
yes, / is divide
ex. 9/3=3
Jul 17, 2009. 1:21 AMDNR says:
how to solve decimal values
Apr 10, 2010. 8:50 PMGuard13007 says:
 Batch files mess up over decimals, blame Microsoft.
Jul 18, 2009. 1:48 PMelmynase says:
good question !!
Apr 10, 2010. 8:50 PMGuard13007 says:
 Batch files mess up over decimals, blame Microsoft.
Apr 16, 2009. 6:00 PMNetworkNinja says:
This is mine, has black letters on green background. Looks kool.

@ECHO OFF
color 20
:blah
ECHO Calculator Version 1.0
ECHO.
SET /p UDefine=
SET /a UDefine=%UDefine%
ECHO =
ECHO %UDefine%
ECHO.
PAUSE
goto blah
Jul 18, 2009. 11:54 PMDNR says:
how to solve decimal values
Apr 10, 2010. 8:49 PMGuard13007 says:
 Batch files don't like decimals, and screw up over them for some reason. Blame Microsoft.
Apr 16, 2009. 7:45 PMNetworkNinja says:
My newer version (it just looks better)

@ECHO OFF
color 20
:blah
ECHO Calculator Version 1.1
ECHO.
SET /p UDefine=
SET /a UDefine=%UDefine%
ECHO =
ECHO %UDefine%
ECHO.
PAUSE
ECHO.
ECHO --------------------------------------------------------------------------------
ECHO.
goto blah

On the v1.0 Neo gave us, there was no loop so you'd have to re-open the batch, and if you insert a "goto" command, there was no gap btwn the title and last line before it looped. I fixed those things.
Jun 2, 2009. 12:30 PMrickyFo says:
when i try this, i get acces is denied I wanted to take a DIR of a Users file on windows, so i typed in... CD C:\Users (to get to the folder, then i typed) C:\Users>DIR>DIRContents.txt (C:\Users> i did not type in, i typed in DIR>DIR Contents.txt) and then it came up with access denied
Apr 10, 2010. 8:48 PMGuard13007 says:
 Access denied means your user acount doesn't have permission to do something you're trying to do. Such as create a file like you are here.
Jul 19, 2009. 12:52 AMDNR says:
dont put space bwn dir>dircontent.txt
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.
1-40 of 148next »

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