330 likes | 805 Views
BATCH COMMANDS. Batch Scripting. What is a Batch File?. These are special files, often called scripts that allow you to run programs from the DOS Command Window. They are useful for storing sets of commands that are always executed together. Batch files end with a .BAT extension
E N D
BATCH COMMANDS Batch Scripting
What is a Batch File? • These are special files, often called scripts that allow you to run programs from the DOS Command Window. • They are useful for storing sets of commands that are always executed together. • Batch files end with a .BAT extension • The process of creating a batch file is called DOS batch scripting.
@ at-sign/ampersand • Hides the echo of a batch command • @[command] • @echo OFF {hides the echo off report}
ECHO • Controls whether commands & comments within a batch file are displayed • echo [ON|OFF|message]
ECHO Usage • echo • echo ON • echo OFF • echo processing… • displays echo status • restores normal display activity • halts display of DOS prompt & commands • displays “Processing…” on the screen
ECHO Usage • echo %USER% • echo. • echo ^L > prn • displays the value of USER on the screen • displays a single blank line on the screen • sends an ASCII control-code (form feed) to the printer. Press <Ctrl> plus <L> to type the ^L character
ECHO Usage • echo Y|Del *.* • Answers the DEL “Are you sure” question automatically
%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 • 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 characters in the environment associated with a string value. Variable is created in the environment by using SET.}
%VARIABLE% Usage • %PATH% • {Returns the value of PATH, the current search path, which is executable.} • echo %PATH% • {Displays the value of PATH, the current search path.}
%VARIABLE% Usage • %PROMPT% • {Returns the value of PROMPT, the current prompt string, which is executable.} • echo %PROMPT% • {Displays the value of PROMPT, the current prompt string.}
%VARIABLE% Usage • echo The current search path is:%PATH% • {Displays the message including the current search path.} • set USER=Johnif %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. • cls
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 %%argumentin (list) docommand • Argument: any letter from A to Z. List: a sequence of strings separated by spaces or commas. Wildcards are allowed.
FOR 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.
GOTO • Transfers control within a batch file to a line identified by a label. The label must be of the form ":LABEL". • gotoLABEL: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 Syntax • if [not] condition command • Condition: errorlevel number; string1= =string2; or exist filename. Command: any DOS command, batch command, batch file name, or program name.
IF Example • if [not] errorlevelnumber 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.
SET • Set will view the DOS environment or create, change, or delete environment values. • set [variable=[value]] • Variable: a string of characters, unbroken by spaces, which are converted to uppercase letters in the environment. Value: a string of characters, case specific, associated with variable.
SET Examples • set • Display the entire DOS environment. • set USER=John • Sets the value of USER to the string, "John". • set USER= • Removes USER from the environment.
SHIFT • Shifts any parameter on the command line one position to the left. Use SHIFT to refer to multiple parameters by one name or to use more than ten parameters on a single command line. • shift
SHIFT Example • :LOOPCOPY %1 A:shiftif not (%1)==() gotoLOOP • Beginning with the first parameter, all the parameters listed on the command line are iterated and a file, the value of the parameter, is copied to A:.
REM • Adds remarks to a batch file. • rem [remark] • @rem • Hides the remark from display.
Batch Commands BATCH SCRIPTING