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!


  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


echo hello


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.


Press any key to continue . . .


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%


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" (


) 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 (



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%


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


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.