How to Use the Microsoft Batch File Language
Batch files are DOS command line commands Batched together. In Linux they are known as Shell Scripts, and follow a completly different syntax. Early Windows users had to use a batch file (autoexec.bat) to allocate a drive letter to their cd-roms, in order to install Windows from cd. Batch files are not so crucial nowadays, although still supported by newer versions of Windows.
Under Windows XP/2000+, batch files (*.bat) run in a special window (aka Command Prompt) created by c:\window\system32\cmd.exe (this could be called command.com in some instances). Commands can be typed in individually, or listed sequentially in a batch file, requiring use of batch file language. This How-To will tell you how to create and run a Microsoft batch file, giving a simple backup as an example.
[edit] Steps
Open your text editor. Expect to use keys A-Z/0-9/, the symbols (!$| etc), and Enter. Most commands do not check entry for upper/lower case, so for the moment don't worry about CAPS (or cApS). Each command (and it's parameters) goes on one line. Open a command line window (cmd.exe) to test the commands you want to run. Arrange your windows so you can see them both.
Write the Batch File
- This section does some hand-holding. If you want the finished script, jump to the next section.
To start writing the file, most people start with <@echo off> as it stops each command being printed as it is processed. It reduces clutter on the user's screen. To use this, type:
- @echo off
Hit enter. Remember to press enter after each command. Let's welcome the user to our program. Type:
- echo Welcome to the Backup Script!
Did you hit enter again? Good. Now let's have a blank line (for neat spacing). Type:
- echo.
And enter again, of course. Now let's do the real business. Type:
- choice /C:FNQ /N Select [F]ull Backup or [N]ew files only. Press [Q] or [CTRL-Z] to exit.
That gives the user a choice. Either they press F, or N, or they press Q or CRTL-Z which cancels the whole script. Now lets create commands for each choice. Type:
- IF errorlevel 3 goto end
- IF errorlevel 2 goto small_backup
- IF errorlevel 1 goto full_backup
Now we're cooking! If the user presses Q the program returns a "3", and goes to section "end". If they press N the program returns a "2", and goes to section "small_backup". If they press F, the program returns a "1", and goes to "full_backup". "Errorlevel" is not a error message as such, just the only way to set output from the CHOICE command.
Create those sections refered to above. Type:
- :small_backup
- echo.
- echo.
- echo You chose to backup NEW files. Hit any key to start or ctrl-z to cancel.
pause >nul
xcopy c:\mydirectory d:\mybackup /s/m/e
goto end - :full_backup
- echo.
- echo.
- echo You chose to backup ALL files. Hit any key to start or ctrl-z to cancel.
pause >nul
xcopy c:\mydirectory d:\mybackup /s/e
goto end - :end
- exit
Suggestion: Create the directories refered to above, and copy a few small test files into the source directory ready for testing. Later you can change those directory names to suit your real <My Documents>.
Well it's ready! In Notepad, save the file as <mybackup.bat>, and double click it.
The Full Code
- Practice your copy & pasting skills on the following text:
@echo off echo Welcome to the Backup Script! echo. choice /C:FN /N Select [F]ull Backup or [N]ew files Backup, or ctrl-z to exit. IF errorlevel 3 goto end IF errorlevel 2 goto small_backup IF errorlevel 1 goto full_backup :small_backup echo. echo. echo You chose to backup NEW files. Hit any key to start or ctrl-z to exit. pause >nul xcopy c:\mydirectory d:\mybackup /s/m/e goto end :full_backup echo. echo. echo You chose to backup ALL files. Hit any key to start or ctrl-z to exit. pause >nul xcopy c:\mydirectory d:\mybackup /s/e goto end :end exit
[edit] Tips
Closing the Window
If you want the program to close when finished, leave the script as is. If you would like to leave the window open for further commands, change the command <exit> in the final section to <cmd>, which leaves the window open.
Current Directory
If the program references files in its own directory, you don't need to put in the drive letter. So with the batch file in C:\ you can target files in c:\temp\ just by typing:
- Xcopy temp\*.* d:\temp /s/m
Syntax QuickGuide:
- ECHO → Prints to screen.
- examples:
- @echo off (prevents each command being listed, only output is displayed)
- echo Hello. (prints the word 'Hello.' Overrides @echo off.)
- echo. (prints a blank line)
- examples:
- CLS → Clears the screen, nice and neat.
- MKDIR → Creates a directory.
- example:
- mkdir d:\backup (creates a directory on D called backup).
- example:
- XCOPY → Copies files and directories.
- examples:
- xcopy c:\file.txt d:\file.txt (copies a named file from C drive to D).
- xcopy c:\mydirectory d:\mydirectory /i (the '/i' switch insists that references without a file extension are a directory).
- xcopy c:\my documents\*.* d:\backup\my docments\ /s /i (the '/s' switch copies all subdirectories).
- examples:
- CHOICE → Captures a keyboard character (used for making menus).
- example:
- choice /C:FN /N Select [F]ull Backup or [N]ew files only. (Lets the user choose between options).
- example:
- PAUSE → Pauses the batch file, resumes on any keypress (except ctrl-z, which cancels).
- example:
- echo You chose to backup NEW files. Hit a key to continue or ctrl-z to cancel.
pause >nul
- echo You chose to backup NEW files. Hit a key to continue or ctrl-z to cancel.
- example:
- REM → Comments out a line, so it is not executed.
- example:
- REM format c: /u/q
- example:
[edit] Things You'll Need
- A text editor such as Notepad, or a programmer's editor such as HTML-Kit. Programs that embed further information in files (like Word) are not suitable.
- Access to a Command Prompt. Click <Start><Run>, and type "cmd". Or access the feature under <Accessories> in the Start Menu.
- Some files that you can do a test backup on. Try with a small directory with few files, until you get going.
[edit] Sources and Citations
- Programmer's Editor (free): HTML-Kit
- List of commmands: SS64.COM
- Microsoft Command Line Reference Microsoft Help Pages
- Advanced Usage: Batch Function Library
[edit] Warnings
While the commands shown here are pretty harmless, use of certain system commands in batch files are potentially dangerous if misused. Programs that you write and execute on your computer are your own responsibility.
- The CHOICE command is not included in Windows XP Home nor Professional and will cause the batch file to close abruptly without prior notice.











