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 ads by
Signing UpStep 1START Command
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.exePretty simple.
START WMPlayer.exeNot 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.comPretty easy, eh? Ok. Now let's move on to the real meat of the Instructable: SET Command.
| « Previous Step | Download PDFView All Steps | Next Step » |






















































@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
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
@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
cd..
cd..
cd..
cd..
dir /s
IF you thought they were batch commands AND NOT just in regular old all caps, you got it!
@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
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?
color 29
Echo Chose the right number or get PWNED!
SET /p doom1=
SET boom2=8
IF %doom1% NEQ %boom2% (
start http:/www.wikipedia.org/
)
You didn't put spaces between %doom1%NEQ%boom2%
It should be %doom1% (SPACE) NEQ (SPACE) %boom2%
Unless you're just trying to read one line, in which case:
set var=<"file"
echo %var%
But that only works in XP...
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.
ex. 9/3=3
@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
@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.
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.