Freigeben über


Making your scripts look prettier

Many languages provide a mechanism to make their code look pretty. VBScript, for example, assumes one statement per line, but including an underscore at the end of the line indicates that your statement continues on the next line.

Normal
WScript.Echo "This is line one" & vbcrlf & "This is line two" & vbcrlf & "This is line three"

With Line Continuation Characters
WScript.Echo "This is line one" & vbcrlf & _
"This is line two" & vbcrlf & _
"This is line three"

It's much easier to tell what's going on in the second script.

In batch, you often see variables "built up" like this:

set MACHINENAMES=sinistar
set MACHINENAMES=%MACHINENAMES% tempest
set MACHINENAMES=%MACHINENAMES% joust

...and in the end you have a variable with three items in it.

As it happens, you can take advantage of CMD's escape character, the carat, to make this a little easier to read:

set MACHINENAMES=sinistar^
tempest^
joust

And again you have a variable with three items in it. The second example does have one (largish) difference, though. Rather than MACHINENAMES being "sinistar tempest joust" (as in the first example), it will have "sinistar tempest joust". If spacing is important to what you're doing, then by all means stick to the first way. However, if you're only building up a list to use in a for loop, the second will work just fine since for splits its arguments on one or more whitespace characters, so the following are equivalent:

for %x in (%MACHINENAMES%) do @echo %x
for %x in (sinistar tempest joust) do @echo %x
for %x in (sinistar tempest joust) do @echo %x

Admittedly, I've only used this once or twice. Generally the lists I have to keep are pretty small, but on the occasions where I have very long lists, it can come in handy.