List of Commands:
Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in the below listing I have listed all commands in all caps to help you identify what is a command and what is not.
Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in the below listing I 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. |
CHOICE | See running different programs for an example of how to use this command. |
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. For an example of this see running different programs |
IF | Used to check for a certain condition if the condition exists. If that condition exists it will perform that function. To see an example of this see running different programs. |
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. |
- Running different programs
- How to start Windows files and programs from a batch file
- Creating a batch file delay
- How to make a time log
Below is a simple example of how you can implement the choice options into your batch files. Each line that is in red can be left out of the batch file. They have been included to help explain some of what the batch file means. Windows 2000 and Windows XP users will need to substitute the choice command with the set command.
@ECHO OFF
REM - LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN
CLS
REM - THE BELOW LINE GIVES THE USER 3 CHOICES (DEFINED AFTER /C:)
CHOICE /N /C:123 PICK A NUMBER (1, 2, or 3)%1
REM - THE NEXT THREE LINES ARE DIRECTING USER DEPENDING UPON INPUT
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
REM - LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN
CLS
REM - THE BELOW LINE GIVES THE USER 3 CHOICES (DEFINED AFTER /C:)
CHOICE /N /C:123 PICK A NUMBER (1, 2, or 3)%1
REM - THE NEXT THREE LINES ARE DIRECTING USER DEPENDING UPON INPUT
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
To run Microsoft Windows programs or files use the START command. The below example would run Windows Notepad.
START /MAX NOTEPAD
You can also specify the direct location of the file by typing the below command.
START /MAX C:\Windows\NOTEPAD.EXE
Below is an example of how to delay a batch fileany where from 5 to 99 seconds. In the below example we illustrate a 5 second delay.
TYPE NUL | CHOICE.COM /N /CY /TY,5>NUL
The below example demonstrates how to create a time log of when the batch file is loaded, or for example, this could be used in the autoexec.bat when someone logs into a computer that supports this file.
ECHO. |TIME > TIME
COPY LOG +TIME
COPY LOG +TIME
An alternate, slightly more complicated method would be the following:
echo @prompt set date=$d$_set time=$t$h$h$h> {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log
Another alternative is:
echo. |time |find "current" >> log
For the above batch file to work properly you must create a file called log, by typing edit log and then save and exit the file, creating a 0 bytes file. If this file is not created or not created properly you will receive the error message Content of destination lost before copy.
No comments:
Post a Comment