Introduction: Getting Into Batch

Have you ever heard of MS-DOS (MicroSoft-Disc Operating System)? If you do then you probably remember typing out commands for the computer to understand. Maybe typing in start (program)? Well if you do you are probably wondering if the MS-DOS style typing in commands and something happens stuff still exists. If so, I would be glad to tell you, it exists!

Step 1: Explain!

You are probably wondering what you have to download in order to do this. But, you don't have to download anything (as long as you are on Windows)! It's called the Windows Command Prompt. And the scripting language it uses is called Batch.

Step 2: The Basics

So now you want to know how the use the Command Prompt, don't you? Well I think you do so let's go over the basics.

First off these commands can be all capitals (ECHO) all lowercase (echo) or a combination of both (EcHo).

echo SOMETHING Displays text (you must use a ^ in front of the ^ and | characters)

@echo off Turns off unnecessary information (like the current directory)

cls Clears the screen of text

exit Exits the command prompt

set Varname=number/Varname Set a variable

set /a Varname=number/Varname+-/*number/Varname Does arithmetic to a variable

if Varname lss(less)/leq(less/equal)/equ(equal)/neq(not equal)/geq(greater/equal)/gtr(greater) number/Varname command Does something based on a variable's contents

:framename A place that holds some commands use underscores ( _ ) as spaces

goto framename Goes to a frame which holds some commands

To call variables use command %Varname%

That's the absolute basics, onto a simple program!

Step 3: Simple Counter

Now you know the basics, let's make a counter.

Go into notepad and type this in:

  • @echo off
  • set num=0
  • :count
  • cls
  • set /a num=num+1
  • echo Number=%num%
  • goto count

Save this as count.bat and make sure that the Text Documents (*.txt) is changed to All Files.

When you double click the count.bat file it should open a command prompt and start count up rapidly.

Congratulations! You made a program! Well unless you just downloaded it...

Step 4: Time Counter

This program is like a stopwatch but you cannot pause the time.

  • @echo off
  • title Time Counter
  • set second=0
  • set minute=0
  • set hour=0
  • :count
  • set /a second=second+1
  • if %second% equ 60 set /a minute=minute+1 && set second=0
  • if %minute% equ 60 set /a hour=hour+1 && set minute=0
  • echo Hour:%hour%
  • echo Minute:%minute%
  • echo Second:%second%
  • ping -n 2 127.0.0.1>nul
  • goto count

And, of course, save as something.bat.

Wait a second, what are these title and ping commands, and what's with the &&?!

The title command changes the window's title (the name up at the top of a window).

The ping command talks to a nonexistent server for a second before doing the next command.

And the && only does a command only if the previous one succeeded (echo Yo && echo John says Yo John)

Similar to the && the || only does the next command if the previous one failed, and the & command just does it's command no matter the previous command's success.

Step 5: Fibbonacci Calculator

This calculates (some of) the Fibbonacci sequence (1,1,2,3,5,8,13...).

  • @echo off
  • title Fibonacci Calculator
  • :start
  • cls
  • set num=1
  • set num2=1
  • echo 1
  • echo 1
  • :loop
  • set /a tnum=num
  • set /a num=num+num2
  • set /a num2=tnum
  • if %num% equ -1323752223 goto stop
  • echo %num%
  • ping 1.1.1.1 -n 1 -w 5000 >nul
  • goto loop
  • :stop
  • pause
  • goto start

You save as normal (something.bat)

You can also change the 5000 in the ping to some different time (in milliseconds).

The if statement is just to prevent overflow (too many numbers).

Step 6: Simple Calculator

You've made some automated things, but we need something that requires human (or robot) input!

  • @echo off
  • title Calculator
  • :start
  • cls
  • echo This is a simple calculator.
  • echo Type in add for addition, sub for subtraction.
  • echo mul for multiplication or div for division.
  • set /p answer= if %answer% equ add goto add
  • if %answer% equ sub goto sub
  • if %answer% equ mul goto mul
  • if %answer% equ div goto div
  • if %answer% neq add goto start
  • :add
  • cls
  • set result=ERROR
  • echo Please type in the first number to add.
  • set /p num1=
  • echo Please type in the second number.
  • set /p num2=
  • set /a result=num1+num2
  • echo = %result%
  • pause
  • goto start
  • :sub
  • cls
  • set result=ERROR
  • echo Please type in the first number to subtract.
  • set /p num1=
  • echo Please type in the second number.
  • set /p num2=
  • set /a result=num1-num2
  • echo = %result%
  • pause
  • goto start
  • :mul
  • cls
  • set result=ERROR
  • echo Please type in the first number to multiply.
  • set /p num1=
  • echo Please type in the second number.
  • set /p num2=
  • set /a result=num1*num2
  • echo = %result%
  • pause
  • goto start
  • :div
  • cls
  • set result=ERROR
  • echo Please type in the first number to divide.
  • set /p num1=
  • echo Please type in the second number.
  • set /p num2=
  • set /a result=num1/num2
  • echo = %result%
  • pause
  • goto start

If you haven't guessed this is a calculator.

Before you go ballistic, the set /p Varname= makes what the user types in a variable, also the set result=ERROR is there to prevent errors causing the last calculated number showing up. For example: I add 2+2 then I do 1/0 (1 divided by 0) it gives an error but it shows 4, but with the set VAR=ERROR when I do that it shows ERROR, instead of 4.

Step 7: Number Guesser

Cool, but anything else but counters and arithmetic? Well yes!

Let's do something a bit random...

  • @echo off
  • title Number Picker
  • set points=0
  • :intro
  • echo Number Picker
  • echo The goal is the get the as many points as you can!
  • pause
  • :setup
  • cls
  • echo how many numbers to choose from?
  • echo 1-10, 1-100 or 1-1000.
  • set /p answer=10, 100, 1000 or quit^>
  • if %answer% equ 10 goto 10
  • if %answer% equ 100 goto 100
  • if %answer% equ 1000 goto 1000
  • if %answer% equ quit exit
  • if %answer% neq 10 goto setup
  • :10
  • cls
  • set /a number=%random%*10/32768+1
  • :10_2
  • cls
  • echo Current points: %points%
  • echo Guess the number (1-10)
  • echo To guess type in a number between 1 and 10.
  • echo You can also quit.
  • set /p answer=^>
  • if %answer% equ %number% goto Win
  • if %answer% equ quit goto intro
  • if %answer% neq %number% goto :10_2
  • :100
  • set /a number=%random%*100/32768+1
  • :100_2
  • cls
  • echo Current points: %points%
  • echo Guess the number (1-100)
  • echo To guess type in a number between 1 and 100.
  • echo You can also quit.
  • set /p answer=^>
  • if %answer% equ %number% goto Win
  • if %answer% equ quit goto intro
  • if %answer% neq %number% goto :100_2
  • :1000
  • set /a number=%random%*1000/32768+1
  • :1000_2
  • cls
  • echo Current points: %points%
  • echo Guess the number (1-1000)
  • echo To guess type in a number between 1 and 1000.
  • echo You can also quit.
  • set /p answer=^>
  • if %answer% equ %number% goto Win
  • if %answer% equ quit goto intro
  • if %answer% neq %number% goto :1000_2
  • :Win
  • cls
  • echo You guessed the number!
  • echo Good job!
  • echo You get 1 point!
  • set /a points=points+1
  • echo Total points: %points%
  • pause
  • goto setup

This is a simple random number generator that makes you guess said random number.

But wait! What is that set /a number=%random%*1000/32768+1command?!

Well, it generates a random number. But by default the set command's %random% variable has boundaries of 0 and 32767, which is way to large for all three of these numbers. So that set /a number=%random%*1000/32768+1 command sets the boundary to 1-1000, you can also change the range to 0-999 by deleting the +1. Or change the 1000 to say 500 to change the range to 1-500.

Step 8: Character Generator

I've taught you how to make a loop, and how to use the %random% variable, how about we combine the two?

  • @echo off
  • title Character Generator
  • :strt
  • cls
  • echo How many characters (abc...) should this generate?
  • echo One character=one byte on your hard disk.
  • set /p num=^>
  • set /a cutof2f=num+1
  • goto generate
  • :generate
  • set /a cutoff=cutoff+1
  • if %cutoff% equ %cutoff2% goto display
  • echo Generating character %cutoff% out of %num%...
  • set /a pre=%random%*30/32768+1
  • if %pre% equ 1 set char=a
  • if %pre% equ 2 set char=b
  • if %pre% equ 3 set char=c
  • if %pre% equ 4 set char=d
  • if %pre% equ 5 set char=e
  • if %pre% equ 6 set char=f
  • if %pre% equ 7 set char=g
  • if %pre% equ 8 set char=h
  • if %pre% equ 9 set char=i
  • if %pre% equ 10 set char=j
  • if %pre% equ 11 set char=k
  • if %pre% equ 12 set char=l
  • if %pre% equ 13 set char=m
  • if %pre% equ 14 set char=n
  • if %pre% equ 15 set char=o
  • if %pre% equ 16 set char=p
  • if %pre% equ 17 set char=q
  • if %pre% equ 18 set char=r
  • if %pre% equ 19 set char=s
  • if %pre% equ 20 set char=t
  • if %pre% equ 21 set char=u
  • if %pre% equ 22 set char=v
  • if %pre% equ 23 set char=w
  • if %pre% equ 24 set char=x
  • if %pre% equ 25 set char=y
  • if %pre% equ 26 set char=z
  • if %pre% equ 27 set char=1
  • if %pre% equ 28 set char=2
  • if %pre% equ 29 set char=3
  • if %pre% equ 30 set char=4
  • set /p="%char%">tempchars.txt
  • :display
  • cls
  • type tempchars.txt
  • del tempchars.txt
  • pause
  • goto strt

This generates a character, saves it to a text file, then does it again, until it reaches the number you inputted, then it displays the text file (type (filename)) and deletes it (del (filename)).

You might be asking "What is that weird set /p command?". That command saves the generated character to a text file, you could do this with the echo command (echo %char%>>tempchars.txt), but that saves the character to a new line.

You're also probably asking how you could add more characters, to answer your question, all you do is add another "if %pre% equ (charnum) set char=(character)" and increase the set /a pre=%random%*30/32768+1's limit by one (in this case changing 30 to 31). Charnum means what random number is changed into the character.

Also; the maximum amount of characters you can generate with this is 2,147,483,647.

Step 9: What Is This Bug?

Why am I asking myself a question? Because it's very relevant to scripting in Batch. The "bug" I am talking about is what I like to call the null text error. The null text error occurs whenever you have a set /p var=, if %var% equ, if %var% neq statement:

  • :frame1
  • set /p variable=^>
  • if %variable% equ a goto a
  • if %variable% equ b goto b
  • if %variable% neq a goto frame1

You may notice that if you press enter at the prompt which asks for input the command prompt spits out an error and terminates. This is because you entered no data into the variable:

  • >
  • (%variable% is not set to anything)
  • (trys to see if a value matches a nonexistant variable)
  • (says the variable doesn't exist)
  • (terminates)

This has two fixes which do the same thing.
Fix one:

  • :frame1
  • set variable=random_stuff/something_that_will_never_be_entered
  • set /p variable=^>
  • if %variable% equ a goto a
  • if %variable% equ b goto b
  • if %variable% neq a goto frame1

Fix 2:

  • :frame1
  • set /p variable=^>
  • if %variable% equ a goto a
  • if %variable% equ b goto b
  • goto frame1

Both methods work well, but I reccomend the second (will work no matter what), but both accomplish the same goal of eliminating this "bug".