Batch File Commands

Here is a ultimate list of batch commands, so that you can understand the making of .bat files
• @
• IF
• Miscellaneous

@ In DOS version 3.3 and later, hides the echo of a batch command. Any output generated by the command is echoed. The at-sign can be prefixed to any DOS command, program name, or batch file name within a batch file.
examples @ {Seperates sections of the batch file without diplaying the DOS prompt.}
@echo OFF {Hides the echo off report.}

%DIGIT Replaceable batch parameters which are defined by the user when the batch is executed. The parameters are separated by spaces, commas, or semicolons.
%digit {Digit: any digit from 0 to 9. %0 has the value of the batch command as it appears on the command line when the batch is executed. %1 represents the first string typed after the batch commmand. Each occurrence of %digit is replaced by the corresponding string from the batch command line.}
examples MYBATCH DOC A:
COPY *.%1 %2 {Copies all .DOC files in the default directory to drive A:}

%VARIABLE% Replaces the DOS environment variable name with its environment value.
%variable% {Variable: a string of uppercase characers in the environment associated with a string value. Variable is created in the environment by using SET.}
examples %PATH% {Returns the value of PATH, the current search path, which is executable.}
echo %PATH% {Displays the value of PATH, the current search path.}
%PROMPT% {Returns the value of PROMPT, the current prompt string, which is executable.}
echo %PROMPT% {Displays the value of PROMPT, the current prompt string.}
echo The current search path is: %PATH% {Displays the message including the current search path.}
set USER=John
if %USER%= =John goto LABEL {Since the value of USER does equal “John”, the control is transferred to the label, LABEL.}

CALL Loads and executes a batch file from within a batch file as if it were a external command. When a second batch file completes, control is returned to the calling file.
call [drive:][path]filename [batch-parameters]
Before DOS version 3.3:
command /c [drive:][path]filename [batch-parameters]

CLS Clears the video display screen, setting the cursor in the upper left-hand corner.

ECHO Controls whether commands and comments within a batch file are displayed.
echo [ON|OFF|message|.]
examples echo {Displays echo status}
echo ON {Restores normal display activity.}
echo OFF {Halts display of DOS prompt and commands.}
echo Processing… {Displays “Processing…” on the screen.}
echo %USER% {Displays the value of USER on the screen.}
echo. {Displays a single blank line on the screen.}
echo ^L > prn {Sends an ASCII control-code (form feed) to the printer. Press plus to type the ^L character.}
echo Y|Del *.* {Answers the DEL “Are you sure” question automatically.}

FOR Repeats the operation of a DOS command for each member of a list. Use CALL to execute a batch file as a command.
for %%argument in (list) do command {Argument: any letter from A to Z. List: a sequence of strings separated by spaces or commas. Wildcards are allowed.}
examples for %%d in (A,C,D) do DIR %%d *.* {Displays the directories of drives A, C, and D sequentially.}
for %%f in (*.TXT *.BAT *.DOC) do TYPE %%f {Types the contents of all .TXT, .BAT, and .DOC files in the current default directory.}
for %%P in (%PATH%) do if exist %%P\*.BAT COPY %%P\*.BAT C:\BAT {Copies all batch files which exist in any directory on the DOS command search path into the directory C:\BAT.}
for %%f in (*.PAS) do call compile %%f {Compiles all .PAS files in the current default directory.}

GOTO Transfers control within a batch file to a line identified by a label. The label must be of the form “:LABEL”.
goto LABEL

IF Tests a condition and executes a command only if the condition is TRUE. But if the NOT modifier is present, the command will be executed only if the condition is FALSE.
if [not] condition command {Condition: errorlevel number; string1= =string2; or exist filename.Command: any DOS command, batch command, batch file name, or program name.}
examples if [not] errorlevel number command {Errorlevel: an exit code returned by a program or an external command. The following DOS commands return an exit code: BACKUP, RESTORE, FORMAT, REPLACE, and XCOPY.Number: a numerical value (integer) against which the exit code is compared. The condition is TRUE if the exit code returned by the previous program is greater than or equal to number. The condition is FALSE if the exit code is less than number.}
BACKUP C:\*.* A: /s
if errorlevel 3 goto TROUBLE {If the BACKUP command exits with a code of 3 or higher, control will be transferred to the label TROUBLE.}
if errorlevel 3 if not errorlevel 4 echo ERROR #3 occurred
if errorlevel 4 if not errorlevel 5 echo ERROR #4 occurred {Nested if statements that determine the exact error number.}

if [not] string1= =string2 command {The condition is TRUE if both strings are identical. The comparison is case sensitive. If either string is blank, a syntax error occurs.}
if (%1)= =(LTRS) CD C:\WORD\LTRS {If the first parameter is LTRS, the change directory to LTRS.}
if “%1″= =”” goto ERROR {If there is no parameter, then control is transferred to label ERROR.}
if not %2X= =X DIR %2\*.* {If there is a second parameter, then display all the files contained in the directory %2.}
if not “%3″= =”” if not “%3″= =”b” if not “%3″= =”B” goto BADPARAM {If there is no third parameter or if it is anything other than b or B, then go to label BADPARAM.}

if [not] exist filename command {The condition is TRUE if filename can be located. The filename can include drive and path specifications. Wildcards are allowed.}
if exist D:\%1\nul CD %1 {Tests for the existence of directory %1 even if it contains no files, then changes to that directory if it exists.}
if not exist A:\FLASH.EXE COPY C:\PROJECTS\FLASH.EXE A: {Copies FLASH.EXE to drive A, but only if it doesn’t exit there already.}

PAUSE Pauses the running of a batch file and displays the message “Press any key to continue …” on the screen. If the optional message is included, it will be displayed first. Use pause to optionally terminate the batch file with at a safe place. The optional message is not displayed when echo is OFF, so the message must be echoed on the preceding line.
pause [message]
examples pause {Displays “Press any key to continue …”.}
pause nul {Redirects command output to oblivion.}
command > file {Redirects command output to file.}
command >> file {Appends command output to file.}
command < file {Redirects file output to command.}
PATH {Displays “PATH=” followed by the value of PATH, the current search path.}
PATH directories {Sets directories as the current search path.}
PATH = directories {Sets directories as the current search path.}
PATH; {Disables extended command searching and confines the searching to the default directory.}
PROMPT {Resets the prompt string to its default, $n$g.}
CD {Displays the current directory and its path.}
. {Represents the default directory (If PATH=D:\;C:\SYS;C:. then current directory will be searched after D: and C:\SYS).}
.. {Represents the parent of the default directory (C:\TOOLS\WP\LTRS.DOC is the same as ..\WP\LTRS.DOC).}
%% {A literal “%”.}

have phun!!!!!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s