![]() |
|
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.
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
:ID4
:sPark
:Original
: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.
| @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
: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). |
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.
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.