Windows Batch

How to program .bat files (Windows Batch Files).

===============================================

TROUBLESHOOTING: If your program immediately disappears, put the command pause at the end.

===============================================

Works on Windows XP and up. Limited functionality is included in Windows 95 and 98.

First, you must open Notepad (Start –> Run –> notepad)
Then, open the file menu and click Save As.
Pull down the Save as Type menu and select All Files (*.*).
Finally, name your file, put .bat after it, and save it anywhere.

Now, to edit the program file you just created, right click on the batch file (.bat) and select Edit.
We’re back in Notepad and it’s time to create the program.

If you want to use echo in your program (display text on the screen) or even if you don’t, it’s a good idea to put the command @echo off at the top of your program. It enables you to use echo without the batch’s path file.

Then you should probably set the title (the top bar) of your program with the title command, and put your title after it, like so:

@echo off
title This is my new program!

Now you can do whatever you want with your program, so let’s slow down and see how the commands work.

echo = displays something on the screen, like this:
echo Hello world.
To skip a line, use echo. .
P.S. Skipping lines in notepad doesn’t make any difference in the actual program.

cls = Clears the screen

pause = displays Press any key to continue…

So our program so far is:

@echo off
title This is my first batch program!
echo Hello world.
echo.
pause
cls
echo I just cleared the screen!
pause

To get someone’s input, use the command set /p (variable name)=(question). For example, you could do the following:

@echo off
title Register yourself!
echo Please fill out the following form:
echo.
set /p name=What is your name?
set /p age=How old are you?
set /p reason=Why are you registering here?
echo Ok. I got it.
pause
cls
echo So:
echo.
echo %name% is %age% years old and registered because %reason%.

Switches coming soon!

Readers Comments (6)

  1. Are you guys open source?

  2. is there going to be more to this tutorial?

  3. Hey Tim I did a LOT of this stuff on my free time at my school so i could help with this stuff.

Leave a comment

Your email address will not be published.


*