Presentation is loading. Please wait.

Presentation is loading. Please wait.

Batch Files Flow of Control to Strengthen Copyright © 2003-2016 by Curt Hill.

Similar presentations


Presentation on theme: "Batch Files Flow of Control to Strengthen Copyright © 2003-2016 by Curt Hill."— Presentation transcript:

1 Batch Files Flow of Control to Strengthen Copyright © 2003-2016 by Curt Hill

2 Flow of Control Like any good scripting language we must have flow of control Batch files have –Subroutines –An if statement –Evil GOTO –A for loop –A choice statement Copyright © 2003-2016 by Curt Hill

3 Subroutines One batch program can execute another by using Call Format: CALL batname parms... –The first will be suspended and the new one started –When the second is done, the first is resumed Notice that CALL is not needed to execute a.exe Copyright © 2003-2016 by Curt Hill

4 Exit statement The exit statement exits the DOS Box The syntax is: EXIT [/B] returncode The returncode is a numeric that is returned to the OS The /B is optional –Leaves the batch command but not the DOS Box –If the batch command was called by another it only leaves the current batch command/file Copyright © 2003-2016 by Curt Hill

5 IF statement Batch files have a simplified if –Reminiscent of BASIC, which itself is watered down FORTRAN Example: Use an environment variable as a batch password –if not %1 == %pwd% exit /B 2 Format: IF condition statement We now ask what the condition and statement must be Copyright © 2003-2016 by Curt Hill

6 What is a statement? Any DOS command Any Batch command Just one –This is FORTRAN IV and before or original BASIC style If –Disgusting Copyright © 2003-2016 by Curt Hill

7 What is a condition? One of three constructions S1==S2 ERRORLEVEL EXIST filespec We may also apply NOT to any of these Copyright © 2003-2016 by Curt Hill

8 Equality condition This condition is usually used to compare a parameter against some constant value, but can be used for other things as well Uses double = (like C), not a single = There must be something on the right and left Copyright © 2003-2016 by Curt Hill

9 Missing Parameters A missing parameter expands to null, so we usually use additional characters: IF “%1” == “C:” These are not required by syntax but are helpful Then if there is no parameters this will expand to: IF “” == “C:” and not a syntax error that would end the batch Copyright © 2003-2016 by Curt Hill

10 The Missing Parameter If we just used: IF %1 == C: And there was no first parameter, it expands to: IF == C: This is a syntax error –Batch files are processed when they are executed, so the syntax error message will come out to the user of the batch file –No compilation, no error checking Copyright © 2003-2016 by Curt Hill

11 NOT We can prefix the condition with NOT and end up with the logical negative IF NOT “%1” == “a:” ECHO Dope! There are no comparisons Recall to compare environment variables, use %: IF %A_VAR% == %1 Copyright © 2003-2016 by Curt Hill

12 Return Codes Every program or command in DOS/Windows has a return code This is the int of C the main function A numeric value that describe what happened Internal and external Not all not all actually use it –The return code may not mean anything By convention 0 means everything is fine Copyright © 2003-2016 by Curt Hill

13 More The DOS prompt does not show us the return code A batch file may check the value and then do something depending on that value ERRORLEVEL is used to look at last return code only –Ony the return code of the last program run Copyright © 2003-2016 by Curt Hill

14 ERRORLEVEL Format is ERRORLEVEL testno –This returns true if the returned number is greater than or equal to testno ERRORLEVEL 0 will be true for zero or positive return codes In practice we test the return codes in a descending order fashion –Large ones first NOT may also precede the condition Copyright © 2003-2016 by Curt Hill

15 One more thing There is also an environment errorlevel variable You may test it directly: if %errorlevel% == 5 … You are not allowed to set it Copyright © 2003-2016 by Curt Hill

16 EXIST condition This is used to determine if a file exists The item following the exist is a file specification The condition is true if the given file does exist NOT may precede condition Copyright © 2003-2016 by Curt Hill

17 GOTO One statement is not enough In C/C++ we have a compound statement –Here we do not –Use the infamous GOTO statement Nobody in their right mind has designed a language that depends on the GOTO since the early 70s –Curse you Bill Gates! Copyright © 2003-2016 by Curt Hill

18 GOTO Again The format is: GOTO label Where label is label in the batch program A label in a batch program starts with a colon (:) and is alone on the line, but in the GOTO you do not use the colon Somewhat similar to MS assembly Copyright © 2003-2016 by Curt Hill

19 GOTO Example Example using FORMAT to interpret error codes: FORMAT A: IF ERRORLEVEL 5 GOTO USERNO IF ERRORLEVEL 4 GOTO FATALERR IF ERRORLEVEL 3 GOTO CTRLC ECHO COMPLETED SUCCESSFULLY GOTO END :USERNO ECHO YOU RESPONDED NO GOTO END :FATALERR ECHOR A FATAL ERROR OCCURRED... :END Copyright © 2003-2016 by Curt Hill

20 Looping Goto can be used to make loops or an if then else construction –This is exactly how it is done in assembly If we conditional branch to later in the file it is usually a conditional If we conditional branch earlier then it is making a loop There is one easier to make loop Copyright © 2003-2016 by Curt Hill

21 For Loops The FOR allows us to run the same command many times The many times is once each for each item in a group or set This is a much more sophisticated loop –Likely added later Copyright © 2003-2016 by Curt Hill

22 The FOR The general format of the for is: FOR var IN set DO cmd where: –FOR, IN and DO are just keywords –var is a variable such as %c –From the command prompt it is %c –set is a parenthesized list of things, usually files Copyright © 2003-2016 by Curt Hill

23 Sets Usually a file specification with wildcards –(*.*) is the set of all files in this directory May have multiple things separated by blanks –(abc.dat xyz.doc stuff.jun) We may have a combination of files with and without wildcards –(abc.* dat??.sss $*.* last.one) Copyright © 2003-2016 by Curt Hill

24 More on For The control variable (the %x or %x) will become each file in this list before being substituted into the command The cmd is a single command usually with replaceable parameter It may be a executable program or bat file For each item in the set we get one execution of the command Copyright © 2003-2016 by Curt Hill

25 For Continued The replaceable parameter should make each execution of the command different Parameters and file specifications may be constant or replaceable parameters Type command does not allow wildcards Execute a type that uses wildcards Type out all files of extension.lst: for %f in (*.lst) do type %f Copyright © 2003-2016 by Curt Hill

26 Restrictions and exceptions The things in the set do not have to be files, but that is usually what they are –Example: execute ls with two different parameters: for %p in (x d) do ls /%p –Execute ls twice: ls /x ls /d Copyright © 2003-2016 by Curt Hill

27 Redirection Redirection and piping may be used, but it does not always do what you want Redirection: for %f in (*.lst) do type %f >stuff.lis –Each separate copy of type z.lst will be redirected to stuff.lis –Thus only the last one is saved Copyright © 2003-2016 by Curt Hill

28 Non Batch The batch language is limited and primitive compared to the UNIX shell It is a common thing in UNIX to execute the shell from inside a program to access an external command The Windows Application Programming Interface (API) allows this as well –More common than lots of batch files are GUIs that execute commands –Such as IDEs Copyright © 2003-2016 by Curt Hill


Download ppt "Batch Files Flow of Control to Strengthen Copyright © 2003-2016 by Curt Hill."

Similar presentations


Ads by Google