1 / 29

BATCH COMMANDS

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

kineks
Download Presentation

BATCH COMMANDS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BATCH COMMANDS Batch Scripting

  2. 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.

  3. @ at-sign/ampersand • Hides the echo of a batch command • @[command] • @echo OFF {hides the echo off report}

  4. ECHO • Controls whether commands & comments within a batch file are displayed • echo [ON|OFF|message]

  5. 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

  6. 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

  7. ECHO Usage • echo Y|Del *.* • Answers the DEL “Are you sure” question automatically

  8. Example

  9. Example

  10. %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

  11. %DIGIT • MYBATCH DOC A:COPY *.%1 %2 • {Copies all .DOC files in the default directory to drive A:}

  12. %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.}

  13. %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.}

  14. %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.}

  15. %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.}

  16. 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]

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

  18. 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.

  19. 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.

  20. GOTO • Transfers control within a batch file to a line identified by a label. The label must be of the form ":LABEL". • gotoLABEL:LABEL

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. 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

  27. 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:.

  28. REM • Adds remarks to a batch file. • rem [remark] • @rem • Hides the remark from display.

  29. Batch Commands BATCH SCRIPTING

More Related