The magazine of the Melbourne PC User Group
Understanding the Backup
Batch File
Tom Coleman
|
 |
A detailed look at the backup batch file described in PC
Update last month.
ECHO OFF
C:
CD\PCW
ED %1
:START
CLS
ECHO DO YOU WISH TO BACK UP THE *.TXT
ECHO AND *.DOC FILES ONTO DRIVE A:
QUESTION
IF NOT ERRORLEVEL 1 GOTO NOBACKUP
ATTRIB C:\PCW\*.TXT +A
ATTRIB C:\PCW\*.DOC +A
CLS
:BACKUP
ECHO IS THERE A DISC TO RECEIVE THE DATA IN DRIVE A:
QUESTION
IF NOT ERRORLEVEL 1 GOTO START
XCOPY C:\PCW\*.TXT A: /M
IF NOT ERRORLEVEL 1 GOTO DOC
GOTO BACKUP
:DOC
XCOPY C:\PCW\*.DOC A: /M
IF NOT ERRORLEVEL I GOTO NOBACKUP
GOTO BACKUP
:NOBACKUP
CLS
ECHO HAVE A NICE DAY |
For those of use who are sufficiently batch file literate the above is nothing special. Those who are still feeling their way might be a bit bamboozled by it
all.
What it does is run PCWRITE in the usual way. Once PCWRITE finishes the batch file takes over again and prods the user to make a backup of the data files.
Lets break it down into pieces and look at them one by one.
The first line
tells DOS not to echo the commands. This is probably best explained by looking at the way DOS operates. The first word on a line is regarded as a command and everything after that as a parameter or a qualifier if you like.
For example DEL FILE.EXT. DEL is the command and FILE.EXT is what the command has to work on.
If you were to type in HAVE A NICE DAY, DOS would check things out for a file called HAVE.COM or HAVE.EXE or HAVE.BAT. If it found one it would try to execute it with the parameters A and NICE and DAY.
works the same way. It executes DIR with A: and /W as parameters.
From inside a batch file all the commands are ECHOED to the screen just as they are ECHOED from the keyboard when you type them in. In addition to this the command ECHO means "Write this" on the screen unless there are some
parameters sending it off to the printer or somewhere. What gets written is the
parameters following the ECHO command.
So unless you turn the ECHO OFF. first the command and its parameters gets written on
the screen then the command executes and write parameters to the screen again.
So it would look like this,
ECHO HAVE A NICE DAY
HAVE A NICE DAY |
Turning the ECHO Now the next three lines
just about explain themselves.
C: changes to drive C:, just in case you were not there already. As you know (don't you) you an returned to the subdirectory that you last were in when you changed from drive C:. So the
command CD\PCW makes sure that you are in the right subdirectory. These commands have no effect is you are in the drive or subdirectory already.
ED %1 is worth a moments thought. We have mentioned commands and parameters. One of the. ways DOS keeps track of what is on the command line is to number the parts. The first thing on the line is numbered 0 , the next 1 then 2 and so on. To enable DOS to know that we are referring to things on a command line we use a % symbol. It. has nothing to do with maths or percentages. It is simply a convenient symbol.
So the Command ED which is the command that fires up ED.EXE the main program for PCWRITE has a parameter of %1.
That %1 is the second item on the command line that fired up the batch file.
Suppose this batch file was called PCWBUP.BAT I could type in PCWBUP LETTERS.DOC. The LETTERS.DOC would become %1 and get passed on to ED in the batch file.
This would make ED fire up with the document LETTERS.DOC in the work place instead of having to make a selection as the program started.
If we don't put a parameter after PCWBUP then there is nothing to pass on as %1 so
ED starts up without it and asks you for a file name later instead.
At this point it is worth noting that simply changing the above commands appropriately we could have
CD\LOTUS and 123 or LOTUS as the command line or CD\WP51 and fire up WP. The same principle applies all the way through.
The next line is
You will see that there are a few one word lines that start with a colon (:). These are labels. They are used to glue a name to a location in the batch
file. Any name will do. It could have been called :IGWFDLKK but :START seemed more appropriate.
Glancing ahead you will see that there are labels
:BACKUP. :DOC, :NOBACKUP. All the labels serve to locate a place in the batch file that DOS :an be
sent to to do something. You will see later that there are GOTO commands here and
there; that direct the flow of control to the appropriate label.
When DOS strikes a label it just notes its presence and carries on to the next line. Nothing happens at
the label.
The next line is
Very useful. It dears the screen. You can execute
that from the DOS prompt any time you like. It may be used to clear the screen of
mistakes. It doesn't feel so bad if you don't have errors staring back at you.
We have already touched on the next two lines.
ECHO DO YOU WISH TO BACK UP
THE *.TXT
ECHO AND *.DOC FILES ON DRIVE A: |
These are messages put on the screen for the user.
Remember that this file we are dissecting is only an example so these messages
would reflect the purpose that you would put a batch file to.
There are two file extensions in here to jog your
memory that some programs, dBase and Lotus for example, will have a number of
data files that must be backed up.
The message could have been put on one line but it
may be more readable an two.
The next line
is interesting. This runs the program QESTION.COM. QUESTION.COM
displays a message prompting you to answer either Y or N. It is not case sensitive.
When the operator puts in a Y the errorlevel is set to 1. An N sets the
errorlevel to 0.
This errorlevel is a most unfortunate choice of words. It suggests errors or mistakes or
things running off the rails. The errorlevel is used by DOS to keep track of how
the last command was going. When a command completes normally the errorlevel returns
to 0 otherwise it is set to some higher figure.
QUESTION.COM tweaks the system and sets the errorlevel artificially.
We can test the errorlevel with the IF function or, as in this case, with the IF
NOT function. When you test for a NUMBER AND ALL NUMBERS ABOVE IT. So if testing
for errorlevel 0 would always get a TRUE answer. Errorlevel answers are always
either TRUE or FALSE.
The next line, which is the one that is testing the
errorlevel,
| IF NOT ERRORLEVEL 1 GOTO
NOBACKUP |
uses a convoluted logic becasue asking "Is it
0" doesn't work. What the above line is asking is "Is it true that it
is not 1 or higher". In other words did the operator put it a N.
If it is true that the errorlevel is not 1 or greater
then the operator has said no to the query about backing up. This causes control
to be diverted to the label :NOBACKUP. From there the screen is cleared and the
message "Have a nice day" is displayed and control returns to the
command line at the DOS prompt as the batch file terminates.
If the operator put in a Y then the ediection to
the label :NOBACKUP has no effect and the next lines are executed.
Here the assumption is that the DOS file
ATTRIB,COM is available. If ATTRIB.COM is in your DOS subdirectory and the DOS
subdirectory is included in the PATH command in your AUTOEXEC.BAT then you will
have no problems. If not you will need to correct that situation. There are alternatives
which will not be dealt with here.
The effect of the next two lines
ATTRIB +A C:\PCW\*.TXT
ATTRIB +A C:\PCW\*.DOC |
is to turn all the Archive attribute bits of the
*.TXT and the *.DOC files ON. A bit of discussion might help here.
It is not hard to understand that a file has
certain properties or functions or states of being such as being READ ONLY or
HIDDEN. Another property or attribute as DOS calls it is that of being USED or
UNUSED.
All this kind of information is stored along
with the file's name in the DIRECTORY AREA where DIR command looks to get the
list of files. It just does not show when the files are listed.
Instead of calling the attribute USED, DOS has
a flag, a bit that is set ON for used and OFF for unused. This is the ARCHIVE
BIT.
Now ATTRIB.COM lets us switch the ARCHIVE BIT ON or OFF.
In this case the +A is a parameter that tells ATTRIB.COM to turn the archive bit ON for all the
*.TXT and *.DOC files. You will see why in a minute.
In some circumstances, discussed later, you might want to miss out these two ATTRIB lines.
The next line clears the screen of the messages so far.
If this line is missed out, the output of the earlier ATTRIB command and some of the later commands will stay on the screen. This is useful if you want to know what is going on. You might want to know which files are going to be copied. The output of the ATTRIB command list them.
The next line is the :BACKUP label. Nothing happening here just yet.
Then there is another message prompting for a disc in drive a: Then another use of the QUESTION command, followed by another errorlevel either/or switch.
ECHO IS THERE A DISC TO RECEIVE THE DATA IN DRIVE A:
QUESTION
IF NOT ERRORLEVEL 1 GOTO START |
In this case if the answer to the question is N then the flow goes back up to the :START label which gives the operator a chance to bail out.
If the answer is Y then the redirection is ignored and the next line is executed
|
XCOPY C:\PCW\ *.TXT A: /M |
This uses the DOS XCOPY command to first of all copy all the *.TXT files to drive A:. Most of the command line speaks for itself except for the last bit. The /M. This parameter tells XCOPY to only copy those
*.TXT files that have the ARCHIVE BIT turned ON and, having made a copy, to turn the ARCHIVE BIT OFF.
The next line tests for a non 0 errorlevel.
|
IF NOT ERRORLEVEL 1 GOTO DOC |
This will happen if the disc was filled before all the *.TXT files were copied. Remember that the errorlevel is a completion code. If the command did not complete satisfactorily then the errorlevel will not be 0
Instead of bombing out it simply passed the flow back up to the :BACKUP label. This time the operator will have the "Insufficient
disk space" error message on the screen and will be once more prompted for a disc in drive A:.
Once the disk has been changed and the operator presses Y then the XCOPY command is executed once more. As the /M parameter turned off the Archive bits of the successfully copied files, the XCOPY command only looks at those files that have the archive bit still on, i.e. those that have not been copied.
This continues until all the *.TXT files have been successfully copied. Then with the
errorlevel set to 0 the flow jumps to the :DOC label, which executes an XCOPY command similar to the previous one.
:DOC
XCOPY C:\PCW\*.DOC A: /M |
If the disk fills this time, the flow once more returns to the :BACKUP label
|
IF NOT ERRORLEVEL 1 GOTO BACKUP |
With a new disk in drive A: the XCOPY *.TXT command is issued again but this time there will be no files to copy so DOS will advise the operator "0 files copied" then flow continues to the second XCOPY command.
Once this completes satisfactorily the batch file continues to termination.
IF NOT ERRORLEVEL 1 GOTO NOBACKUP
GOTO BACKUP
:NOBACKUP
CLS
ECHO HAVE A NICE DAY |
There are a couple of ways we could vary this batch file.
The most obvious change is to merely alter the filenames and subdirectories to back up other program's data.
A slightly different approach would be to make a complete backup copy of all the data files and then execute the ATTRIB command with a -A parameter, turning off all the Archive bits. You would then have a situation the same as you have at the end of a successful completion of this batch file.
You would have a complete copy of all data and all the archive bits would be turned off:
You can now delete the two ATTRIB lines. In future DOS will turn on the archive bits of those files as you use them without any intervention on your part.
Now this batch file will only back up those files that have been accessed and presumably changed.
You might want to add a message here and there that Ctrl/C or Ctrl/BREAK will stop the batch file command from executing.
You could use a batch file similar to this one to load a file up into a ram disc. When you were finished it would copy it back to your hard
disk. Then it could offer you the option of making a backup copy also.
The question arises "What happens if I want to make a choice of more than two options".
There are other utilities besides QUESTION.COM and some of these QUERY.COM for instance, will let you choose any letter or number, and set the errorlevel to almost anything. It gets fiddly testing for every
errorlevel but it can be done. It requires more patience than skill.
There is TASK.COM which is a TIMED ASK. It lets you answer Y or N within a user defined time limit. If it received no reply within the limit it sets the error level to 2. It sets the errorlevel at 1 if you respond N. The opposite of QUESTION.
They do things like that to keep us from getting bored.
The BBS and the PD Software Library are full of useful batch file utilities like the ones discussed here. There are plenty of others there too. You can put a highlight bar menu in a batch file with a couple of them.
Incidentally PCWRITE turns out pure ASCII text and is perfect for wilting batch files, unlike most other word processors.
Reprinted from the July 1991 issue of PC Update, the magazine of Melbourne PC User Group, Australia
|