Complex Batch Files(Otherwise known as making the simplest of tasks as confusing as possible)
You should know the basics of how to write batch files. As your knowledge of DOS commands grows, so too does the available commands you can use in your batch files. However, there are some commands that can only be used in batch files, or at least only effectively in a batch file. They are some of the most powerful tools available in batch. They are:
IF - Executes code only if a certain condition is true.
SET - Assigns a variable; in a batch file, can also trigger a special prompt.
LABEL - A code block given a special name for reference purposes.
GOTO - Code which allows you to go to a specific label.
FOR - Code which is executed a certain number of times based on certain conditions.
IFIF executes a code block only if a certain condition is true. Typically used with the GOTO command, it makes an excellent way of directing the "traffic" or your code. Example: Let's say you want to do a DIR only if there are files in the directory. This line of code would work:
IF EXIST *.* DIR
The IF EXIST function checks to see if the specified files exist. It also can be used to check to see if they don't exist - IF NOT EXIST.
IF can also be used to see if an operation worked/can be done. If you want to copy a file but you want to make sure the destination directory exists, use ERRORLEVEL. ERRORLEVEL is normally 0, but will become 1 if ever something goes wrong. So:
COPY blah.txt yada\yada.txt
IF NOT ERRORLEVEL 0 ECHO Error: Could not find destination
More info on IF covered later.
SET - Just like in the prompt, set makes a variable that remains until the end of the DOS session (meaning typing the SET command after the batch is executed will show this new variable). Example use: What if you wanted to change directory to the A: drive, and back? Well, how would you know where "back" is, if you have 100+ directories on your PC? And what about other PC's? You can't just guess at it. You can do this:
SET CurrDIR=%cd%
A:
[do stuff]
cd %CurrDIR%
The %cd% variable is the current directory. But when you change directories, %cd% changes. However, saving the %cd% in another variable that does not change allows you to go back to where the user was before.
SET can also be used to make custom prompts, allowing the user to make choices. An example: User can choose between opening AUTOEXEC.BAT, going to A:, or going to C:. Choices can be displayed with the ECHO command, and SET is used to create a custom prompt:
ECHO What would you like to do?
ECHO.
ECHO 1. Open AUTOEXEC.BAT
ECHO 2. Go to A:
ECHO 3. Go to C:
SET /P CHOICE="Enter 1/2/3>"
The /P switch creates a prompt, and CHOICE is a variable. The value in quotation marks is what is displayed at the prompt. When the user types something and presses ENTER, the variable is changed to whatever they entered. Because of this, you can use the IF command to check if the number that was entered is valid. It also allows you to GOTO a certain label based on what was chosen. For this example, string comparison will be used.
String comparison is simply comparing two variables or two strings or one variable and one string to see if they are equal.
IF "%CHOICE%"=="1" AUTOEXEC
IF "%CHOICE%"=="2" A:
IF "%CHOICE%"=="3" CD C:\
LABEL - The best (and only) way to effectively manage what code gets executed, and when. Labels are made by putting text on a line of its own and preceding it with a semicolon (:). It should be one word only. Examples are:
:Begin
:ExecuteCopy
:Error
:1
:Orange
:Zebra
They can really be anything. They are used with GOTO.
GOTO - Used to "go to" the specified label. So, if your code read like this:
GOTO End
ECHO Hi!
:End
You would never see the word "H1!" as it is skipped. This can be used to create sections of code that handle errors, other sections to check for errors, etc.. It's a good idea to put the label "End" at the end of all your batchfiles, so whenever you need to terminate the batch file, you can just GOTO End, and you will effectively end the batch file.
Continued on next post...