|
The command echo followed by the option off sets batch echoing to off, this means that the batch commands will not be echoed to standard output, that is, you will not see them on your screen. The line is prefixed by a "@" character which is used to specify that the line it is present on should not be echoed to standard output, hence is needed so that we do not echo the command sequence that turns echoing off.
|
|
:: A batch file that concatenates all the files of a given extension in
:: the current directory to a specified file and then zips that file.
|
Comments in batch files are officially meant to begin with the prefix REM which stands for REMark, this tells the command interpreter to ignore this line. It turns out, however, that it is quicker to use :: in order to markup a comment. :: is used because : indicates the start of a label, the next : tells the parser to ignore this label.
|
|
IF "%1"=="" GOTO USAGE
IF "%2"=="" GOTO USAGE
|
In batch programming, command line parameters are referenced by using the %n notation where n indicates the numerical weight of the parameter on the command line. %0 refers to the name of the batch file itself, %1 the first parameter, %2 the second and so on. You may only reference a maximum of 9 parameters in this way, if you need more you should process these first ones and then use the shift command to access the rest, the shift, shifts the parameters left by one so that %2 becomes %1 etc, hence the non-existent %10 would be shifted into %9 and you could access the parameter. There is no way to recover a parameter once it has been shifted out completely, i.e %0 will disappear upon being shifted.
The program listing illustrates the use of the IF clause to take an alternative course of action if no either or both of the required parameters is missing. %1 and%2 are compared to an empty string which would indicate their omission from the command line if true, hence if the IF clause evaluates to TRUE the command after is executed which is GOTO USAGE which indicates that program control should jump to the USAGE label.
|
|
FOR %%i IN (*.%1) DO type %%i >> %2
|
This for construct enables us to implement iterative behaviour in our batch files. It says that for each %%i within the set *.%1 we should perform the commandtype %%i >> %2.
%%i is a for-loop variable, these begin with %% and end with a sequence of alpha characters. The for-loop says to assign the values in our set to this variable in turn, which allows us to reference each of the values in the set with the same variable in the for-loop body allowing us to perform a specific operation on each of the values in the set. The specific operation in this case is to redirect the output of type (which dumps the contents of a file to stdout) to a file, specifically %2which is our second command line parameter. The use of two > characters in series ensures that the contents of the file are appended to the end of the target file and do not overwrite the output file, which would be the case if only one > had been used.
|
|
The shift command shifts all command line parameters one parameter to their left, see number 3 for more details about shift. The reason it has been used here is explained in the next callout.
|
|
Pkzip is a shareware file compression program that is executed from the command line, for details on obtaining PkKzip see Auxiliary files for extended batch programming, it takes the command form:
Where zipfile is the name of the zipfile you wish to create and file(s)... are the files you want to put in the zipfile. In order that the zipfile be given the same name as the second parameter (without the extension) passed to the batch file, the notation %~n1 was used to specify that %~n1 should take the value of the name of %1 but not the extension. The notation %2~n1 is not allowed so to reference just the filename of the second parameter passed to the batch file and not the extension, the command line parameters were first shifted to the left by one using SHIFT, as explained previously, the second parameter became the first parameter and it's filename could be referenced with %~n1. Pkzip was told to use the filename %~n1.zip as the zipfile and %1 as the file to compress, which (owing to the SHIFT) was the second parameter passed to the batch file.
|
|
Labels are specified by prefixing an alpha-character identifier with a ':' character, labels are jumped to using the GOTO.
|
|
echo.
echo USAGE: %0 extension outputfile
echo.
echo Concatenates all the files of the given extension in the current directory
echo to the specified outputfile and then compresses that file into a zip file.
echo.
|
By using echo text may be echoed to standard output so that the user may see it. echo. echos an empty line to standard output. This section of the program is only executed if one or both of the command line parameters are omitted. The program shows that normally execution will be caused to circumvent this section by means of GOTO END immediately prior its declaration.
|