top

Home   |   Tips   |   Forum  |   Links   |   Site map

  Computer Hardware 

  Computer History
  Hardware Listing
  Questions & Answers
  Buying Tips
  Help Installing Hardware
 
  Computer Software

  Software Listing
  Virus Information
  Security Tips
  Operating Systems
  MS-DOS
 
  Tips & Tricks

  Networking Tips
  Database Tips
  Registry Tips
  Internet Tips
  Printing Tips
 

 
  Tools

  Print this page

 

  Search The Web

 
   

This Site The Web

            
  NAVIGATION
 

Tips & Tricks
Windows Tips
MS Office Tips
Internet Troubleshooting

Forums
Linux Tips
Security Tips
Glossaries
 
  Batch File

In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. A simple batch file does not require any special programming skills and can be done by users who have a basic understanding of MS-DOS commands.

Example

This example batch file is a simple program that displays a hello world program and then waits for the user to press a key before ending.

@ECHO off
ECHO Hello World!
PAUSE
ECHO on

To execute the file it must be saved with a .bat or .cmd extension in plain text format (with a program like Notepad.

Result

When executed (either from Windows Explorer or Command Prompt) this is displayed:

Hello World!
Press any key to continue . . .

Advanced batch example - conditional shutdown

@echo off
echo 1.Shutdown
echo 2.Quit
set /p choice=enter your choice 1,2: 
if %choice%==1 goto shutdown
if %choice%==2 exit
 
:shutdown
cls
set /p sec=enter the number of seconds that you wish the computer to shutdown in: 
set /p msg=enter the shutdown message you wish to display: 
shutdown -s -f -t %sec% -c "%msg%"
echo shutdown initiated at %time%
set /p cancel=type cancel to stop shutdown 
if %cancel%==cancel shutdown -a
if %cancel%==cancel goto start

Batch commands

Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in the below listing we have listed all commands in all caps to help you identify what is a command and what is not.

@ Does not echo back the text after the at symbol. This is most commonly used as @ECHO OFF to prevent any of the commands in the batch file from being displayed, just the information needed.
%1 The percent followed by a numeric value, beginning with one, allows users to add variables within a batch file. The below line is an example of what can be used in a batch file.

ECHO Hello %1

When the above one-line batch file is created, add your name after the batch file. For example, typing myname (being the name of the bat file) and then your name:

myname bob

would output:

Hello bob

Note: This can be extended to %2, %3, and so on.

:: One of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run.
:LABEL By adding a colon in front of a word, such as LABEL, you create a category, more commonly known as a label. This allows you to skip to certain sections of a batch file such as the end of the batch file. Also see GOTO.
CALL This is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.
CLS Just like the DOS command would clear your screen.
ECHO Will echo a message in the batch file. Such as ECHO Hello World will print Hello World on the screen when executed. However, without @ECHO OFF at the beginning of the batch file you'll also get "ECHO Hello World" and "Hello World." Finally, if you'd just like to create a blank line, type ECHO. adding the period at the end creates an empty line.
EXIT Exits out of the DOS window if the batch file is running from Windows.
GOTO LABEL Used to go to a certain label, such as LABEL. An example of GOTO would be to GOTO END.
IF Used to check for a certain condition if the condition exists. If that condition exists it will perform that function.
PAUSE Prompt the user to press any key to continue.
REM One of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run.
SHIFT Changes the position of replaceable parameters in a batch program.
START Used for Windows 95, Windows 98, and Windows NT 4.0 and above to start a windows application; such as START C:\WINDOW\CALC would run the Windows Calculator. Users running Windows 3.x need to utilize the WIN command.  For example, WIN C:\Windows\CALC.EXE would run Windows and then Calculator after Windows has finished loading. 
 

Bookmark and Share


  Related Links

Computer hardware tips

 

 

 

 
 User's Favourite Stuff

  Batch File Help
  Keyboard Shortcuts
  Missing NTLDR
  Boot Disk Creation
  Beep Codes
 
  Informative Links

  Safe Mode
  Windows Help
  Linux Help
  HTML Color Codes
  Email Tips
 
  Tips & Tricks Cont.

  Backup Tips
  Wireless Tips
  Cisco Router Tips
  PC Cleaning Tips
  Miscellaneous Tips


 

 


Main Site Navigation:
Home   |   Back   |   Site map   | Glossaries |   Forum