Download presentation
Presentation is loading. Please wait.
Published byRandall Gardner Modified over 8 years ago
1
Batch Files More flow of control Copyright © 2003-2016 by Curt Hill
2
Introduction We have already seen the standard commands: –Echo –Using variables –If and GOTO Now we consider some more advanced commands –Looping –Choice Copyright © 2003-2016 by Curt Hill
3
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 an if-like structure If we conditional branch earlier then it is making a loop There is one easier way to make loop Copyright © 2003-2016 by Curt Hill
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
Choice Choice is a batch command that asks the user to choose a character then signals which was typed This allows a batch file to get user input from the console The chosen character then corresponds to the ERRORLEVEL returned There are several option letters that make it work –We see these on next screen Copyright © 2003-2016 by Curt Hill
12
Choice options /C - the characters available –These are not quoted –First one will be signaled by ERRORLEVEL 1, second by 2 –Default is YN –You cannot type something not in the list /CS makes it case sensitive /M – is the message –This is quoted Copyright © 2003-2016 by Curt Hill
13
More options /T – timeout seconds /D – default to return if timeout /N – hide the choices Copyright © 2003-2016 by Curt Hill
14
Example Copyright © 2003-2016 by Curt Hill choice /c ABCD /m "Choose letter" if errorlevel 4 goto d if errorlevel 3 goto c if errorlevel 2 goto b if errorlevel 1 goto a goto endit :a echo A was chosen goto endit :b
15
Finally We should now do some examples We should have a copy of lc, the line counter Lets count lines of C++ code in a single directory Copyright © 2003-2016 by Curt Hill
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.