Friday, October 19, 2007

Label at the End of Block

One thing I hate about batch file is, there is no real language definition you can refer to (or is there?).  So a lot of things I have to find out by myself the hard way.

Here's an example of this:

@echo off

for /L %%i in (1,1,10) do (

  if %%i GEQ 5 goto :NOPRINT

  echo %%i

  :NOPRINT

)

This gives an error saying that ") was unexpected at this time".  It is obvious what this means, and it is obvious how to fix it.  Just put a comment between the label and the closing bracket.

@echo off

for /L %%i in (1,1,10) do (

  if %%i GEQ 5 goto :NOPRINT

  echo %%i

  :NOPRINT

  REM can't have label immediately preceding closing bracket
)

I know this is not really anything useful, just a rant, that's all.

Evil Delayed Expansion

Wait, what's this?  How come something so useful as delayed expansion (see previous posts here and here to see how useful it is) be evil?  Well, it's not the feature itself that's evil, but rather the way you can end up spending a long time trying to figure out what's wrong if you make a simple, stupid mistake.

What mistake am I talking about?  Consider this:

@echo off

setlocal enabledelayedexpansion

set COUNT=0

for /L %%i in (1,1,10) do (

  set /A COUNT=%COUNT% + 1

  echo !COUNT!
)

Simple enough, you said.  And you must have spotted the bug (if you have not, try to run it and see what happens).  Now, of course if the loop body is much more complicated it would not be as easy.  You see, the problem is that what you are doing is completely legal.  Having %COUNT% in the loop body is fine as long as you don't expect it to be delay-expanded.  It's a feature, but when you make that silly mistake (or more likely, someone else in your team), then it's not going to be pleasant to hunt the problem down.

Wednesday, September 26, 2007

Using WaitFor

In Vista, there is a command called waitfor, which as the name suggests, waits for a signal.  It is also the command to send the signal to awaiting waitfor instances.  Since waitfor comes with a timeout option, it's a good substitute for sleep (which, until I found out about waitfor, really was the number one thing I could not understand why it's missing from standard Windows command).

waitfor /T 10 SomeSignal

The above command will simply times out after 10 seconds.

OK, I lied.  There is actually a real substitute for sleep in Windows.  It's the timeout command.

timeout /T 10

That command does the exact same thing as the waitfor example, but you can cut short the sleep for waitfor with some signal, whereas for timeout you cut it short by pressing a key.

But obviously the purpose of the waitfor command is to allow you to start some long running command, do some other shorter things, and  wait for the long running command to finish before continuing.

REM Let's pretend we need to setup something

start cmd /c "DoSetup.exe & waitfor /S %COMPUTERNAME% /SI ThisSignal"

DoSomethingElse.exe

waitfor /T 60 ThisSignal

The first waitfor sends the signal, the second waits for the signal.  Of course you want to make sure DoSomethingElse.exe actually finishes before DoSetup.exe for this to work.  Otherwise the second waitfor will just timeout.  And I'd advise against waiting forever in the second waitfor, since if your DoSetup.exe fails or crashes and exits early, you'll most likely end up waiting too late for a signal that's already sent.

Saturday, August 4, 2007

Adding Accounts To Local Admin Group

Well, OK, it's not only for adding accounts to local admin group.  You can use the commands I'm about to show here to add accounts to any local group.  But I use it most often for making some account a local admin on a machine.

It's a pretty well known command actually.  Say I want to add my domain account as local admin on a machine.

net localgroup Administrators MYDOMAIN\batcheero /ADD

Or you can do the same thing to add another group to local admin.

net localgroup Administrators SomeOtherGroup /ADD

So yeah, you already know that, of course.  But what if you have a service running on some remote machine under that machine's local system account, and you want that service to have admin access to your local machine?  Well, for that, you need to add the machine account of the remote machine to your local machine.  How do you do that?  Well to do that, you use the command ever so slightly differently.

net localgroup Administrators MYDOMAIN\remotemachine$ /ADD

Notice the $ sign at the end of MYDOMAIN\remotemachine$.  This tells the command it's a machine account, and not to be confused with a regular user account, where there is no $ sign at the end.

I know what you're asking.  Why oh why, dont' they just make another switch to say that it's a machine account?  Well, like always, that would be too easy now wouldn't it?

Wednesday, July 25, 2007

Regex Using Findstr

OK, so I'm reeeeally busy these days, and I only have time to post a lame-ass topic like this one.  I mean, who wouldn't know about using findstr to search for regular expressions in a file, right?

Anyway, to do that you all you have to do is pass the /R option, and put the regular expression as /C option argument.  Say you want to search for every lines in a file that begins with a Foo and ends with a Bar, with any string in between.

findstr /R /C:"^Foo.*Bar$" some.txt

To make it search recursively in a directory, use the /S option.  If you want to ignore case, use the /I option.

OK, gotta go.

Wednesday, July 18, 2007

Turning Off Screen Saver from Registry

I have found myself in a situation where I would like to have my screen saver deactivated.  I know, you can do it from the display property just fine, but sometimes I want to be able to do that from the command line so I can make that part of a setup script that I run for all my machines.

There are a couple of registry keys you need to tweak to turn it off.

HKCU\Control Panel\Desktop\ScreenSaveActive  :  "0"

HKCU\Control Panel\Desktop\SCRNSAVE.EXE      :  ""

The first one is the one that really matters.  You are basically turning it off just by setting that to 0.  But this will leave whatever default screensaver in the second registry, and that has the rather undesirable effect of showing that as the active screensaver if you open your display property.

Setting the second registry to an empty string basically just sets the active screensaver to none, which is consistent with how you would do it from the display property.

To actually set those values up in the command line, you can put the registry keys and values in a .reg file the way I did it in the Delayed Expansion article, or you can do it using the reg utility.

reg add "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ /d "0" /f

reg add "HKCU\Control Panel\Desktop" /v "SCRNSAVE.EXE" /t REG_SZ /d "" /f

The /v switch specified the value (not to be confused with data), /t specifies the type of that value, and /d specifies the data you want to set the value to.  We also want to do /f so it does not prompt us when it sees that the values already exist.  Do a reg /? to find out more about the reg command.  And to check if they're indeed modified, you can use the reg query option.

reg query "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ

reg query "HKCU\Control Panel\Desktop" /v "SCRNSAVE.EXE" /t REG_SZ

The meaning of the switches are as before, except this time we don't use the /d and /f option.  If you need to check if a particular value is set or not, you can simply pipe it to a findstr and see if it finds the value you are looking for.  For easiest result, you might want to use the findstr regular expression option.

reg query "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ | findstr /R /C:"ScreenSaveActive.*0"

if "%errorlevel%" EQU "0" (

  echo Screensaver is off

) else (

  echo Screensaver is on

)

As you can see, reg is a pretty useful command to get and set registry keys and as long as you know what to change, it will get the job done for you.

Friday, July 13, 2007

Dynamic Variable Name

Sometimes I find myself missing the ability to use arrays in batch scripts. OK, a lot of times. So to get around that, I often have to resort to using variable names that start with the same string and end with numbers. Like VAR1, VAR2, etc.

Now I know that is lame but it gets the job done. But what if I want to create these variables dynamically? I mean, if I want to read a file, and assign each line in the file to a separate variable, I need to know how many lines there are in the file, otherwise I'm screwed. Right? Well, not quite.

What I find is, some people quite often missed the fact that you can use variable expansion as a name of a new variable in batch. I guess that's because it's rather counter intuitive from other programming languages.

Anyway, the following example reads a file, assign each line to a new variable name and prints the file back, line by line, in reverse order.

@echo off

setlocal enabledelayedexpansion

set COUNT=0

for /F "tokens=*" %%n in (foo.txt) do (

  set /A COUNT=!COUNT! + 1

  echo %%n

  set LINE!COUNT!=%%n

)

for /L %%c in (!COUNT!,-1,1) do (

  echo !LINE%%c!

)

First of all, you need to enable delayed expansion for this to work (see my earlier post about that). Then you want to treat everything on one line as one value, so you use the "tokens=*" option for the firs for loop.

In that first loop you maintain a count, and create a new variable named LINE!COUNT! in each iteration, and assign the whole line to that new variable. See how simple that was?

The second loop just iterates through the count backward using the for /L option, and print the variables in that reverse order. Now take a look at how I'm printing the line. I use !LINE%%c! which uses both the ! and the % to allow the proper expansion of the variable name.

You can also replace the second loop with some other thing like string replacement.

echo. & echo Replace all 'h' with 'y' & echo.

for /L %%c in (1,1,!COUNT!) do (

  set CURRLINE=!LINE%%c!

  set CURRLINE=!CURRLINE:h=y!

  echo !CURRLINE!

)

Voila! Quick and dirty poorman's implementation of UNIX tr command. Of course you don't need to even save the lines to their own separate variables to do that. You can just do the replacement as you read the lines, and print the line out all in the same loop iteration. But hey, that's why they're called examples.

Wednesday, July 11, 2007

Never Edit A Running Batch Script

If you have a long running batch script, you should be careful to never edit and then save the newly edited file while the script is running.

Consider the following simple example.

@echo off

:START

echo hello

pause

goto START

echo oops

goto :EOF

Now run it, and while it's waiting for your input to continue, open the file again, and remove the first line, @echo off and save the file.  Now once you give your input to let it continue, instead of executing the next line after pause, which is goto START, it executes the next line after that, so you will see this output.

hello

Press any key to continue . . .

oops

The reason it skips a line is because you just deleted a line.  The batch processor executes your script line by line, and it does not load your script in memory when it starts.  Instead, it remembers which line it needs to fetch next, and when it finishes executing the current line, it reads the file again and gets that next line.  Now obviously changing the content of the script file while you are executing it will give you undesirable execution order of your script.

Sunday, July 8, 2007

Environment Variable Editing

Say you have and environment variable, and you want to tweak the value, change it a little bit, get the first few letters, or whatever.  You can do this using command processor's built-in variable substring and string substitution feature.

This is really useful feature that I like to use all the time.  Here's an example of how I can compose my own date string in the YYYY.MM.DD format out of the regular %DATE% environment variable that I covered here.

set MY_YYYY=%DATE:~-4%

set MY_MM=%DATE:~4,2%

set MY_DD=%DATE:~7,2%

set MY_DATE=%MY_YYYY%.%MY_MM%.%MY_DD%

The first line takes the substring of %DATE% starting from the 4th character from the end, which gives me the year.  The second line takes the two characters starting from position 4.  Similarly on the third line, I got the two characters starting from position 7.  The last line simply concatenates them together into the format I wanted.

I can use substring to determine if today is Sunday for example.

if /I "%DATE:~0,3%" EQU "Sun" (

    echo Today is Sunday

)

So that shows you how to do substring.  Another cool usage of this is in doing string substitution.  Here's an example where I am replacing the year with my own string.

set MY_DATE=%DATE:2007=9999%

This will replace the year 2007 with 9999.  You can also simply leave the substitution string empty to remove a string from the variable value.

set MY_DATE=%DATE:2007=%

That will remove all occurrences of 2007 from the value.  I usually find this really handy to remove certain directories from my PATH.

You can also use this for testing if a certain string exists in a variable.  Take a look at the following example where I want to see if today is Sunday like the earlier example, but using substring.

if "%DATE:Sun=%" EQU "%DATE%" (

    echo No, today is not Sunday

) else (

    echo Today is Sunday

)

As you can see, what I did there is remove the string Sun from the date, and if it is successfully removed, it means today is Sunday, and the condition will become false.  If it does not find the string Sun, the condition will be true and we know today is not Sunday.

Friday, July 6, 2007

Never Set Errorlevel

What I mean is, don't ever have an environment variable named errorlevel. That's just a bad idea. Windows command processor will take your definition every time. This means if you set your errorlevel, subsequent check for any command's errorlevel will not work.

set errorlevel=0

call some_command_that_returns_non_zero.cmd

if "%errorlevel%" EQU "0" (

echo SUCCESS

) else (

echo FAIL

)

You will always get errorlevel set to 0 or whatever value you happen to have it set to. So the check in the above snippet will always return success.

Well, OK, I admit. When I said above that your subsequent errorlevel check will not work, I lied. The other way of checking errorlevel will still work.

set errorlevel=0

call some_command_that_returns_non_zero.cmd

if errorlevel 1 (

echo FAIL

) else (

echo SUCCESS

)

That would still work. But just save yourself some trouble and stay away from it.

Perl In Batch Clothing

As someone who writes a lot of scripts, Perl is my language of choice. But it does not mean I can live without Windows batch. Sometimes though, it is necessary to combine the two by disguising a Perl script as a batch file.

When might this come in handy? Well, to be honest I can't really think of any right now, but this is certainly cool. This trick is not a batch file trick per se. It's really a Perl trick, and you'll see soon enough why that is. But I'd like to put it here just the same because hey, it's got some batch scripting in it.

To write your Perl script as a batch file, all you have to do is add a few lines at the top of your Perl script, rename the script extension to .bat or .cmd, and you're done.

@echo off

perl -x %~dpf0 %*

exit /b %errorlevel%

#!perl

use strict;

print "Hey $ARGV[0] I'm in Perl!\n";

exit 0;

This file, being a batch file, is executed by the command processor as any batch file. It reads each line and executes that line.

The first line is straightforward. The second line is really the trick. Doing perl -x tells Perl to read the file you pass as argument, %~dpf0, which is the complete path of the batch script, and ignore anything in the file until it finds the #!perl line. Then it treats it like any other Perl script.

The last argument on that line is simply passing all the command line arguments you pass to your batch script to the Perl as well.

So try and invoke the script. Let's say you named the script foo.bat.

foo.bat Batcheero

You'll see the following printed output.

Hey Batcheero I'm in Perl!

Like I said, not much real use I can think of, but it's nifty all the same.

Monday, July 2, 2007

Function Call In Batch

In any programming language, you can write functions or procedures to factor out a piece of code you use often.  In batch you can do the same.  Two ways you can do it.  You can create a separate batch file, and call it from inside the batch file you're scripting, or you can make it as an in-file function.

To call a separate batch file, it's really simple.  All you have to do is precede the batch name with the call operative, and you're good to go.  And example I like to use is to print a log entry both on console and into an output file.

REM PrLog.bat

REM  This batch file prints the message to the console and to an output file specified in the environment variable OUTP

echo %*

echo %* >> %OUTP%

REM End of file

Now every time we want to use this in a separate batch file all we have to do is call it after making sure you initialize the output file.

REM Initialize OUTP just once

set OUTP=%TEMP%\output.txt

del /y %OUTP%

REM Subsequently you just call the PrLog.bat with the message as arguments

call PrLog.bat Hello world 

Now this is nice and dandy, but you now have two files to watch for.  And if you need to copy your script, you need to make sure you don't forget to copy PrLog.bat too.

The more integrated way of doing this is to simply put the PrLog function in file.  To do this we need a label preceding the function, and end it with a goto :EOF or an exit /b.

REM Initialize OUTP just once

set OUTP=%TEMP%\output.txt

del /y %OUTP%

REM Subsequently you just call the :PRLOG function label with the message as arguments

call :PRLOG Hello world 

goto :EOF

REM End of main.

 

REM Start of function PrLog

:PRLOG

echo %*

echo %* >> %OUTP%

goto :EOF

REM End of function PrLog

Note that the batch processor treats the :PRLOG as its own batch context, meaning when you goto :EOF at the end of that function, you don't go to the end of the current batch file, but you simple exit the function's batch context, which essentially means a function return.  You can also do an exit /b from the function if you need to return a meaningful return value to the caller signifying success or fail.

Friday, June 29, 2007

Robocopy

One other utility that Vista comes with which is way better than previous Windows is the robocopy utility.  It's got a lot of options and features, but one that I find very useful is the mirroring option.

With the mirroring option you can create exact copy of the source, meaning that if there are files in the source that are different from the destination, they will be copied, if there are extra files already in the destination that don't exist in the source, they will be deleted.

robocopy /MIR source destination

Try it out.  Play with it.  You'll like it.

One thing you want to watch out about robocopy however, is the error code it returns when it exits.  It's not the straightforward zero == success and non-zero == fail.  Oh, no.  That would be too easy.  So here is a list of the various error codes that robocopy gives out.

Error code Meaning
0 No change
1 Files were copies successfully
2 Extra files deleted from destination
4 Some mismatched files detected
8 Some files could not be copied
16 Fatal error

 

The error codes form a bit field, though.  So 3 really means 2+1, meaning there are extra files but copy was performed successfully.  That means actual failure is anything greater than or equal to error code 8.

robocopy /MIR source destination

if %errorlevel% geq 8 echo FAIL!

Once you know that, it's a sweet utility to have around.  If you're not on Vista, it is included in the Win2k3 Resource Kit Tools which you can download.

Symbolic Links

Symbolic links have existed for ages in UNIX, but Windows have not had any built in support for symbolic links until Vista.  Vista has a utility called mklink, which allows you to create symbolic links for files and directories.  Just be mindful that it requires admin privileges to run.

Symbolic links is useful for creating aliases for files or directories.  Say you do a daily build of something.  And you want to create an alias directory called current to point to the latest build you did.  For simplicity, let's assume your build version is stored in some file called buildver.txt.  You increment that every time you do a build.  You create a directory named after the build version, and you create a current directory pointing to that.

if not exist buildver.txt echo 0 > buildver.txt

set /P CURRBUILD=<buildver.txt

set /A CURRBUILD=%CURRBUILD% + 1

md %CURRBUILD%

pushd %CURRBUILD%

REM the following assumes you have build.bat in your path that will create your directory content

call build.bat

popd

mklink /D current %CURRBUILD%

This way a script that wants to access the current build can just always hit current without trying to figure out what the latest build version is.

If you want to delete it, you can safely use the usual rd command to remove directory.  This will only delete the link, not the actual directory it points to.  The same goes for file symbolic links.  To remove it you use the del command and it will delete the link, not the file.

You can find out if a file is a link or not when you do a dir in a directory by the <SYMLINK> or <SYMLINKD> type it shows you.

For XP and Win2003, you can use a Sysinternals utility called Junction to create NTFS reparse point that will do similar job as a symbolic link.  However unlike symbolic links which will work on files too, junctions only work for directories.

Another tool that can create junctions is the linkd utility from Win2k3 Resource Kit Tool which you can download.

Thursday, June 28, 2007

Setting Variable Value From File

Let's say you have file containing some string, and you want to get that string and assign it to an environment variable.

echo Hello World > foo.txt

There are two ways you can assign the content of that file to an environment variable.  One is to use redirection.  To do this, you need to make the set command interactive by using the /P switch.

set /P BAR=<foo.txt

echo %BAR%

If you have more than one lines in the file, this will set the variable value to the first line in the file.

The other method is using the for loop, telling it to read from the file.

for /F "tokens=*" %x in (foo.txt) do (

  set BAR=%x

)

The "tokens=*" option tells it to consume the whole line, so that if you have any white spaces in the line, it will not pick up the first word only.  If you use this approach and there are more than one lines in your file, your variable will end up with the last line as its value.

Wednesday, June 27, 2007

Printing Date and Time

What's this, you say?  We're getting down to such dumb topics after only a few posts?  Well, it might be dumb, but it's kind of useful.  Say you want to print the date and the time.  You would use the date and time command, obviously.  And they have the /t switch to allow no interaction which is nice for batch files.

date /t

time /t

Excellent.  Marvelous.  Jolly good.  But what if you want to put both of them on the same line?  Like if you want to create a log trace where you put date and time at the beginning of each line?  Well, the date and time commands won't really work well because they will put the date and time on different lines.  Luckily there is an alternative.

echo %DATE% %TIME% - Log message goes here.

This will print out the date, time and log message on one line.  Ideal for logging traces.

Random Number Generation

If you ever need to get a random number while writing a Windows batch script, you can use Windows command's handy little %RANDOM% environment variable.

echo %RANDOM%

And if you want to generate a random number within a range of values, all you have to do is perform some simple command line math.  Say you want to generate a random number between 50 and 150.

set MIN=50

set RANGE=100

set /A RN=%RANDOM% % %RANGE% + %MIN%

That should do it.

Tuesday, June 26, 2007

How To ENABLEDELAYEDEXPANSION

ENABLEDELAYEDEXPANSION is a useful property that allow you to do what you think should happen when you write a for loop or an if block. Consider this example.

set COUNT=0

for %%var in (1 2 3 4) do (

  set /A COUNT=%COUNT% + 1

  echo %COUNT%

)

Now in any other scripting or programming language, this would be just fine. Not so in windows batch. Since batch processor treats the whole for loop as one command, it expands the variables once and only once, before it executes the loop. So you end up with %COUNT% being expanded to its value, which is 0, before you start the loop, and you end up printing 0 four times in a row.

That's where delayed expansion comes in. As the name suggests, delayed expansion makes batch processor delay expanding the variable to its value until it actually loops through it.

But to make that happen, you need to do two things.

1. Enable delayed expansion. You can do this by doing setlocal ENABLEDELAYEDEXPANSION at the beginning of your script.

2. Use ! instead of % to expand environment variable value.

Now here's that example again using delayed expansion:

setlocal ENABLEDELAYEDEXPANSION

set COUNT=0

for %%var in (1 2 3 4) do (

  set /A COUNT=!COUNT! + 1

  echo !COUNT!

)

This time you will see 1 2 3 4 being printed, as you expect.

Another way of turning on delayed expansion is through the registry. You can go and add it manually through regedt32.exe, or you can load a registry file like the one shown here:

Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]

"DelayedExpansion"=dword:00000001

Just save it to a file called enabledelayedexpansion.reg or something, and just invoke that file from the command line.

enabledelayedexpansion.reg

This will enable delayed expansion for current user. You can do the same for HKEY_LOCAL_MACHINE.
You need to start a new console window to see its effect. To check, simply do

echo !COMPUTERNAME!

in the new command console. If it prints your computername, then it recognizes the ! variable expansion, which means delayed expansion is enabled.
This way you don't have to do setlocal ENABLEDELAYEDEXPANSION. But watch out, you need to make sure this thing is enabled if you are planning on skipping setlocal ENABLEDELAYEDEXPANSION in your script.

I'll get into how to check for this from your script some other time.

The Little Pipe That Could

So pipes can be bad at times (see my last post), but pipes can be really good for a lot of things too. I won't go and say a lot about the goodness of pipes.
Here I just want to give an example of how pipe can be used to get you through some of those old fashioned commands that just refuse to be invoked without user interaction.

In windows, comp.exe is one of those things. It's similar to diff, which is not a standard Windows utility. You invoke comp.exe by giving it two file names to compare. But after it compares the files, it will ask you if you want to compare any more files. It ALWAYS asks you that. There is no way to turn it off by passing some command line switch.

So what do you do? Well, one thing you can do feed it a file containing the letter "N" through it's input redirection.

echo N > answer.txt

comp file1.txt file2.txt

This works well.  But what if you don't want to create a temporary file just for answering that? You can use pipe of course. And since you need comp.exe to consume the answer, you just need it at the end of the pipe.

echo N ¦ comp file1.txt file2.txt

Nice and simple. And like I mentioned in previous post, this will get you the exit code of comp.exe itself if you decide to check for errorlevel afterwards.

Monday, June 25, 2007

Hidden Danger of Pipes

OK, this is my first post, so I figure I need to explain a bit what I intend this blog is about. Batch. That's what I intend to write. Specifically, tips, tricks and pitfalls of windows batch file.

Now that we have that out of the way, let's take a look at the first of such batch quirk.

When you are using pipes to do some chained processing, just remember that the exit code of the last command in the pipe is the one that prevails.
I've seen this bitten a lot of people:

test.bat ¦ tee.exe outputfile.log

exit /b %errorlevel%

The intention here is to exit with whatever the exit code of test.bat is, when in fact you're capturing exit code of tee.exe, which will most likely be zero.
You'll then end up masking the test.bat exit code. If this is in some pre-checkin test script, that usually ends up with bad codes sneaking in.
So save yourself some time, and output to a file instead, and post process the output.

test.bat > outputfile.log

set EXITCODE =%errorlevel%

type outputfile.log

This captures your intent just the same (well, almost the same).