Windows BATCH Scripting Loops
Total Page:16
File Type:pdf, Size:1020Kb
Windows BATCH Scripting_Loops Table of Contents Loops ............................................................................................................................................... 2 Loops Multiple File Example ........................................................................................................... 5 Loops Directory Example ................................................................................................................ 8 Loops Multiple Lines in a File Example ........................................................................................... 9 Loops Running a Command Multiple Times ................................................................................. 10 For Loops ....................................................................................................................................... 11 Tokenizing Loop Example -1 ........................................................................................................ 13 Tokenizing Loop Example -2 ........................................................................................................ 16 Notices .......................................................................................................................................... 17 Page 1 of 17 Loops Loops A core component of scripts – allow one thing many times Windows batch scripts use “FOR – DO” and has 4 different constructs depending on which switches are used with “FOR” • Multiple Files – perform one command on each file FOR %a IN (file(s)) DO (commands) • Multiple Directories – perform one command on each directory FOR /D %a IN {dirpath} DO (commands) • Multiple Lines IN a File – perform one command for each line FOR /F %a IN (filename) DO (commands) • Run a Command Multiple Times – perform command many times FOR /L %a IN (x, y, z) DO (commands) Loop variables ARE case sensitive. 36 **036 There is a looping construct within BATCH. The FOR loop. It's one of the core components of scripting. It's very powerful. When you think of being able to do multiple things rapidly, repetitively, you have to do some sort of looping. In this case, this will be the FOR loop. Uses the FOR - DO. And there's, like, there's four different constructs depending on the switch you use for the type of result or the type of looping that you'd like to have happen. On the first one here, when you have multiple files and you'd like to perform a command on each of Page 2 of 17 them, you go ahead and do the FOR. So the key words are FOR, IN and DO. The construct for this is that this particular variable, the %a, can actually be any letter. It doesn't have to be a "a". but I would like to point out, the difference between loop variables and the user made or user declared variables is that loop variables are case sensitive. If you accidentally type capital A% it will not give you the results that you're looking for. So this is a key point to make note of is that if you do have a looping variable, make sure when you go to output it, if you're going to do something with it a little bit later, make sure it's the same one. Again, it doesn't have to be a letter by itself. Could be something else, but... And then you definitely need the FOR, the IN and the DO portions of it. And then you'll want to put the files within the parentheses, file or files. I mean, if it's only one you can do multiple commands to that one file as well. If you have multiple directories that you would like to do something with, perform a command in each of those directories, you use the FOR /D. Same basic format with the variable here, and then IN, the directory path that you'd like to, excuse me, like to loop through, and then the commands at the end that you would like to have run. So if you have multiple lines in a file that you would like to perform a command against, you would use the /F FOR loop. And again, the same Page 3 of 17 basic format, but instead of the directory path you'll put the file name and for each of those lines it'll go ahead and run that command for you. And it knows to stop with the end of that particular file so you don't have to worry about it continuing on. And then there's a, if you have to run a command multiple times, and perform it multiple, there is this FOR loop with a /L. And so there's a little- -this is somewhat similar to a more traditional FOR loop, and this is where the numbering sort of thing happens. But the x, y, and z are actually the value you'd like to start at, the step, and then the stop value that you'd like it to stop at. So this could be like 1, 1. So just keep iterating to, like, say 10, and it'll just do 11 times, right? So just remember the three S-T's if you will. The start, the step and the stop is what you'll need in here. If you need to, you know, count by fives, if you need to count by some other value, you can go ahead and do that in here and it'll run those commands based on that. And you can use the, this variable, becomes the value of whatever this number starts at or during the iterations it becomes a number that this step tells you, this NEXT. So if you need to reference it and use it, if you're counting and you want to show that number as it's iterating, you can reference with the %a and it'll show the value as it's looping what--as it steps up or... And you can use that as you need to in your script. Page 4 of 17 Any questions on this? Ah, yes, Franklin? Student: You have multiple files and you have files in parentheses. With the file, are they separated by a comma or space? Instructor: Yes. You would put a comma between if you have multiple, that's correct. Any other questions? Okay. Loops Multiple File Example Loops Multiple File Example Note the double percents Output (%%) – use in a batch file, but not on the command line Wildcards could also be used here instead of actual file names. 37 **037 Okay. So here is the example of exactly what--well, I apologize. I seem to have misspoken. I thought it was a Page 5 of 17 comma that separates those, but it's a in this case FOR and the %%a. So there is a difference when you use the information from a BATCH file directly into the command line. You only need a single percent before these looping variables. The interpreter, the reason for this, is when you use it in a BATCH file, when the interpreter goes through the very first time, it immediately tries to assign it a value. So that first percent is almost like an escape character, if you're familiar with escaping a particular keyword or character. It's kind of like escaping that second param and telling it, "Hold off right now. We're going to use it a little bit later." So take a step back on that one for just a second. But that's the reason for the double % within this script. And so you have your three different, in this case, three different files that you'd like to do these particular things for and then there's another IF construct. If that file exists, you go ahead and echo out the file name. ELSE. If it's not, the File NOT Found, and the name of that that found. So it is possible to use wildcards here, meaning that if you would like to do it for all but .tmps you can do the *.tmp or *.doc or whatever it is that you would like to do these on. You don't necessarily have to put every single one of those out there. You can, if you have a particular extension that you want to do the commands to, you can use that. Page 6 of 17 So you see the output here. Course, you don't know what's in this particular directory, but it found these two and it did not find the last one, file.tmp. Yes? Student: What's the significance of the indentations or anything? Is that just for aesthetics? Instructor: It is aesthetics, in a sense, but it's also readability. It just lets you know that FOR loop is going to run those things. That's correct. And that's pretty much a best practice. It's... Student: I didn't know if it had a function other than that. Instructor: Right. Student: Okay. Instructor: Yeah. Correct. Some languages explicitly like tabbing or certain things happen based on that. In this case, it really wouldn't matter if everything was pretty much all in a line. Course, the readability at that point goes out the window. So this is definitely for helping the, understanding how it runs, and what happens in this particular loop. Page 7 of 17 Loops Directory Example Loops Directory Example : Output 38 **038 Excuse me. Here's one for directories, multiple directories. Or the /D. Lets it know that it's this type of FOR loop. And in this case, it's actually looking for anything, any directory, that starts with a capital, or a letter "V," I should say. Sorry. That's what the V* is for. And then it'll go and echo out V Directory Found. And it'll actually, every one of those that it finds, excuse me, will be output to the screen. And in this case it found the VMs directory and the vTest directory. That can be helpful as well. Page 8 of 17 Loops Multiple Lines in a File Example Loops Multiple Lines in a File Example : Output Input File 39 **039 So here's one. Multiple lines within a file that you would like to do a certain thing to or run commands against. The /F lets the interpreter know that this is the type that you'd like.