10
Windows BATCH Scripting_Variables Table of Contents Variables -1 ..................................................................................................................................... 2 Variables -2 ..................................................................................................................................... 4 Special Variables ............................................................................................................................. 6 Variable Examples ........................................................................................................................... 7 Notices .......................................................................................................................................... 10 Page 1 of 10

Windows BATCH Scripting Variables - USALearning · Windows BATCH Scripting_Variables. ... Not used in the true sense of the word with Windows batch ... in programming languages

  • Upload
    hakiet

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Windows BATCH Scripting_Variables

Table of Contents

Variables -1 ..................................................................................................................................... 2

Variables -2 ..................................................................................................................................... 4

Special Variables ............................................................................................................................. 6

Variable Examples ........................................................................................................................... 7

Notices .......................................................................................................................................... 10

Page 1 of 10

Variables -1

23

Variables -1

Not used in the true sense of the word with Windows batch scripting

Can be assigned and read from

SET – sets a variable to a particular value

%varname% – references the variable

Can store strings or numbers, but there is no difference in how to set them

• Windows will interpret the value as a string or a number based on what it actually is.

Names are case-insensitive

**023 Okay. Variables. I think I mentioned FOR, are not true variables in the sense, like, they have in programming languages. They can be assigned with the keyword SET. And you would have the variable and then an equal, which is actually the assign symbol. And then if you are going to reference that variable, you want the same name and then you'll have the two % symbols on either side, well, on both sides, I should say. Not on either side, but both sides. In order to reference it. And they can store strings or numbers. And there's really no difference in how to set either of those. And Windows will

Page 2 of 10

interpret the value as a string or a number, just based on what's there. There's no real special, like I said, no real typing that's necessary. And in this case, this is very important. These names, the variables that you come up with, will be case insensitive. So if you have varname someplace and you use it and you do a capital "V," Varname later, the interpreter will treat them as the same thing. So be aware of that if you're thinking of doing something. You'll have to have actually completely different or maybe a varname1, varname2 type of thing in order to use the similar variables.

Page 3 of 10

Variables -2

24

Variables -2

Usage notes• Do not use % when assigning to a variable• Use % when referencing a variable• To clear a variable, set it to nothing• Another character, typically a quote, is used around variables

to do a string comparison• Replaced with their actual value when the command is run

– To have a variable inside a loop that changes value, use “Delayed Expansion”.

– Use !var! to force a replacement during each iteration of a loop.

**024 Okay. So some usage notes here. Careful that--if you don't do this a lot, when you assign, you don't need the % symbols. It's only when you reference that you need the % symbols on both sides. To clear a variable, you SET and then, like, varname and use = and you can either leave a blank or you can do a "" with no space between it and it'll actually fill it with nothing, if you will. So if you have to clear it, that would be the case. And then if you're going to do a string comparison, you'll want a character, usually they use a quote on both sides of the variable, so the

Page 4 of 10

%%, %varname%. If you're going to do a string comparison of that with another value, you'll want to put a, in this case, saying typically it's a quote, but it could certainly be exclamation points or something else on the outside before you do the = equals, or whatever other comparison that you're going to do. That way the interpreter will know to treat it as a string. Okay. So this is a interesting kind of- -I don't know what you want to call it. Condition that you should be aware of when you're doing looping and you set a variable to increase or decrement. You'll want to use what's known as delayed expansion on these, because BATCH scripts are interpreted and so it actually, when the interpreter runs through it the very first time it sets all the variables. And if it doesn't have the delayed expansion it'll go in, it'll set that variable the first time, and the second time through the loop it will not reset that variable. So it's very important if you are going to do that kind of looping, you'll definitely want to set it with the delayed expansion. And then there's another piece here where you can set with your variable, you put the two exclamation points to force the replacement for each loop of an iteration. Or each iteration of a loop, I'm sorry.

Page 5 of 10

Special Variables

25

Special Variables

%ERRORLEVEL% - tracks error from the last command run

• Usually 0 when no error occurs– It is dependent on what the programmer did.o Sometimes 1 can mean “no error”.

• Usually a positive number when an error occurs – Use this number to find a solution online – IF – the programmer

documented it!

**025 Okay. So BATCH scripting, the OS interpreter does have some special variables. This one is the ERRORLEVEL variable. It tracks an error, what error happens after a command is run, whether it's successful or it failed or in what condition it failed in. To access it you do the same % on both sides. Generally speaking, when it returns 0 it's usually a success. And sometimes it's dependent on the programmer of the particular command that wrote it. Unfortunately it's somewhat inconsistent. It's not always the case. Sometimes 1 is the no error.

Page 6 of 10

So again, this is when you want to do your testing beforehand. And usually the positive numbers are when errors occur. So if you're, you just, you just want to make sure when you go through and you make these and you're making assumptions in your conditional statements, you make sure you perhaps put an echo out or something and see what that is before you use it out on live networks and such, and that way you won't have to redo anything.

Variable Examples

26

Variable Examples

> SET myvar=1 Sets myvar to 1

> SET myvar=Sets myvar to nothing, or empty string

> echo %myvar%Prints the variable to the screen

> Echo The Last Error Number Was %ERRORLEVEL%Prints the error number from the last command run

**026 Okay. So here's some examples of setting variables and referencing them. Top one there is

Page 7 of 10

the keyword SET and whatever your variable name happens to be and the equal sign and the 1 is set to 1. In this case, since it is the number 1, it would actually set it to the numerical 1, not the string 1. And if you do need to clear a variable, in case you need to use it again or you need to do a certain comparison, that's where you set it to blank or you can do the quotes with no space between them, two sets of quotes. And then in referencing them you just, you know, in this case we echo. That will print the value within the variable, the two % signs on the outside. And then in this case, this is just a sentence above it, or a sentence before it, I'm sorry. The Last Error Number Was and then you'll be referencing that ERRORLEVEL. And so you'll want to do this immediately after a command was run. You don't want to wait, you know, a couple down and expect it to be the same. It'll be the one that's run right before you call that sets it, so... Chris Evans: A good best practice here when you're building your scripts for the first time, and testing them out, after every command that you run, use that bottom line there that says echo out the error level. You want to understand how your commands are actually working and whether they're actually returning the values that you think they should be returning. So to the previous slide, Wayne talked about the error level isn't consistent among programs and among, you know, how the programmers built

Page 8 of 10

them. So as you're going through and building a script for the first time, always echo out the error level after every command so you know whether it succeeded or not and you actually understand what those values are. It'll save you a bunch of frustration down the road when you're trying to figure out why your script is misbehaving. Because maybe you were checking for one value of error level and the program's actually returning something else. So as you, again, as you're troubleshooting your scripts, that error level echoing that out is your friend and it will save you a lot of time and frustration if you understand that. Instructor: Definitely. Thank you very much.

Page 9 of 10

Notices

6

Notices

© 2015 Carnegie Mellon University

This material is distributed by the Software Engineering Institute (SEI) only to course attendees for their own individual study.

Except for the U.S. government purposes described below, this material SHALL NOT be reproduced or used in any other manner without requesting formal permission from the Software Engineering Institute at [email protected].

This material was created in the performance of Federal Government Contract Number FA8721-05-C-0003 with Carnegie Mellon University for the operation of the Software Engineering Institute, a federally funded research and development center. The U.S. government's rights to use, modify, reproduce, release, perform, display, or disclose this material are restricted by the Rights in Technical Data-Noncommercial Items clauses (DFAR 252-227.7013 and DFAR 252-227.7013 Alternate I) contained in the above identified contract. Any reproduction of this material or portions thereof marked with this legend must also reproduce the disclaimers contained on this slide.

Although the rights granted by contract do not require course attendance to use this material for U.S. government purposes, the SEI recommends attendance to ensure proper understanding.

THE MATERIAL IS PROVIDED ON AN “AS IS” BASIS, AND CARNEGIE MELLON DISCLAIMS ANY AND ALL WARRANTIES, IMPLIED OR OTHERWISE (INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, RESULTS OBTAINED FROM USE OF THE MATERIAL, MERCHANTABILITY, AND/OR NON-INFRINGEMENT).

CERT ® is a registered mark owned by Carnegie Mellon University.

Page 10 of 10