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 » |



















































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.