Free Web Hosting | free host | Free Web Space | BlueHost Review

Variables in Batch Files

Most people who shy away from batch files do so because they are reluctant to touch upon low-level programming, which using DOS basically is. Those who do write their own BAT files to automate tasks know it can be a lot simpler than it looks. However, most stay away from those elements that are balancing on the edge of real programming, such as the use of variables.

Using the IF statement, it is possible to create batch files with different sets of commands to be executed. Admittedly, most of us don't need to use them, as writing a batch file for each set of tasks is no great hassle. However, if you have wondered how to make batch files with which you can use your own switches (or variables), this is the answer. That is all a variable is basically: something typed in after the command, and is represented in batch files by "%1".

In essence, you can cram many small batch files into one, as well as add a messure of safety to batch files you don't want to be accidentally opened. Still not impressed? OK, we will look at two different batch files that use IF: one to change your Startup logo, and another to restore the Registry.

Startup Logo Changer

Let's say you have three startup logos, as well as the original, and want to write a batch to switch between them. First of all, make sure all logos including the original are saved with unique names (like logo.LedZep.sys, and logo.old.sys for the original Microsoft one - this way you can easily tell which images are startup logos). Where they are saved makes no difference, but in this example they are saved in the same folder as the startup logo (ie: C:\).

The best way to explain it all is to just jump right into an example. Have a look at the batch below (which we will call Logo95.bat), then read the explanations to see what is happening.
 
Now, let's look at the elements of this batch. The first 4 lines use the IF statement, each pointing to an appropriate label (the labels are those lines that start with a colon [:]). Each label has a copy command under it; the goto end command after each just finishes the batch file after the command is executed (as end has nothing under it). The fifth line of the batch, goto end, means that if the batch file is run without a variable, it will go straight to end (which just ends the batch).

But what is this variable business? OK, typing Logo95 will run Logo95.bat, but, as we have established, doing so without a variable will do nothing (same goes for double-clicking the BAT file). But let's say you want to change the Startup logo from whatever it is to your South Park logo. As you can see, the command to overwrite logo.sys with logo.SouthPark.sys rests under the sPark label.

Look amongst the IF statements for sPark (third line). There it says that if the variable ("%1") is sp (meaning you have typed sp after logo95 at the DOS prompt or as the target of a shortcut), then goto the label sPark and see what commands need to be run. In this case, the South Park logo is copied over the Startup logo, then the batch jumps to end, which just ends the batch as there are no commands under it.

IF "%1"=="led" goto LedZep
IF "%1"=="id4" goto ID4
IF "%1"=="sp" goto sPark
IF "%1"=="old" goto Original
goto end

:LedZep
copy c:\logo.LedZep.sys c:\logo.sys
goto end

:ID4
copy c:\logo.id4.sys c:\logo.sys
goto end

:sPark
copy c:\logo.SouthPark.sys c:\logo.sys
goto end

:Original
copy c:\logo.old.sys c:\logo.sys
goto end

:end

For an even better understanding of what is going on, I will explain the sequence of events.

After typing (or running a shortcut to) logo sp, the batch goes to the first line and says:
IF "sp"=="led" goto LedZep
Since sp is not led, it goes to the next line
IF "sp"=="id4" goto ID4
This causes it to go to the third line
IF "sp"=="sp" goto sPark
A match has been found, so the batch directs itself to goto the sPark label without reading anymore IF statements. The file is copied and the batch goes to end, which is empty, and the process is finished.

So, if you run the batch without any variable after logo95, it will go through the IF statements like this:
IF ""=="led" goto LedZep
Since no variable is not the same as led or any of the others, it will keep going till it hits the fifth line, goto end, at which point the batch will end without having done anything. This can be a great little security feature for batch files that could do damage if run accidentally. The next batch file is one every PC should have tucked away somewhere, just in case Windows refuses to boot, but it is not one you want ot run by mistake.

Registry Restorer

@echo off
if "%1"=="restore" goto Restore
echo  ******* To Restore the Registry, Type REGFIX RESTORE at the DOS Prompt *******
echo Please Note That This Will Overwrite Your Windows Registry With an Earlier Backup
goto end

:Restore
cd %winbootdir%
attrib -r -h -s system.dat
attrib -r -h -s system.da0
ren system.dat system.bad
ren system.da0 system.dat
attrib -r -h -s user.dat
attrib -r -h -s user.da0
ren user.dat user.bad
ren user.da0 user.dat
goto end

:end

Now we will look at a batch file that restores the Registry from its previous backup. I would like to call this useful, but hopefully you will never actually need it. Still, knowing the right commands could save a reinstall of Windows, and having a batch handy that can do it all in a flash makes it easier. This batch does not need variables to perform the tasks, but we will use IF to add a measure of security (so that if anyone double-clicks the file by accident, the commands will not be executed). Let's call the batch regfix.bat, and make it so that if run without a variable, all that happens is a line of text is displayed.

If you do not type restore after regfix, it will run the echo command which displays some text explaining this. It will then goto end so that the commands under the Restore label are not executed - if you do not specify otherwise, it will continue through the commands (treating the :Restore line as an invalid command or filename).

If you run regfix restore, it will go to the Restore label, then change to your Windows folder and replace your 2 Registry files (system.dat & user.dat) with the last backup (which Win95 makes automatically & leaves with the da0 extension).

Variables and Shortcuts

OK, so many of you haven't even seen the DOS prompt let alone use it often, so isn't all this a bit fiddly? After all, you could just create separate batches for each logo change or whatever, and run them from shortcuts (for easy access). Well, if you are going to the bother of writing batch files and making shortcuts to them, you might as well save even more time. We'll look at the logo changer batch as an example.

So, you have made a batch that lets you change the Startup logo with any of four available. Create four shortcuts to regfix.bat, then rename them to reflect which logo each will change to. Right-click the first one, choose Properties/Shortcut, and type a space and one of the variables after logo95.bat under Target. Do the same for each shortcut and variable, then all you have to do is click an icon and it's done!

Shortcuts to executables like BAT files are saved as PIF files, and have DOS-specific options when you open Properties. There you can force the batch files to close on exit, and even open minimized. Another alternative, however, is to have one single command under the end label within the batch itself: exit.

This will close the DOS box, which means trouble for batch files like the Registry Restorer (where text is to be displayed if run without the restore variable). The text would still be shown, but going to end would run exit and close the box before the message could be read. So, in this type of batch, the approach would be to keep the end label blank, and replace goto end under the other label(s) with exit. In this way, DOS boxes vanish after completed tasks, while leaving them open for messages if no variable is specified.

%WINDIR% - Another Type of Variable

Want to make a batch file that will install files to the Windows folder of any machine? Well, when you specify the target folder, which you would usually put down as C:\Windows, simply put it as %WINDIR%. Your system knows where your Windows directory is, and will supply the path when a batch file asks for it via the %windir% variable. The files will then be moved or whatever to the Windows folder, whether it's path is C:\Windows or C:\Win95 or otherwise.

What if you need the System folder to be the target, or any of the folders inside the Windows directory? Just put the path as %windir%\system (if you leave out the \ [backslash], the batch file will report that the file was successfully copied to C:\WINDOWSsystem, which is wrong as the folder does not exist).

As you can see, this variable can make installations to other PCs' Windows folder easy and flawless. But even if you only make batch files for your own system, %windir% can save you a few seconds of typing.


Click Here to Go Back to CyberFrank's Batch FilesClick Here to Go Back to CyberFrank's PC RealmBack to the Weird World of Frank (Index Page)

Click Here to Send Mail to CyberFrank