QuestionI want to distribute my batch file. How can I make sure users don't mess up my batch file?
AnswerIt's a fact of life that if it is possible for some knucklehead to mess it up, he/she will. There are three main methods of making sure it doesn't get botched up: 1) Explain how it works before the user uses it, 2) make a help section of the batch, and 3) add protection code to ensure that the user cannot screw up.
1. Create a readme fileInclude a readme to go with your batch file, explaining the syntax, its uses and limitations. This, however, is the least reliable method, as very few people actually read it, no matter how much you tell them to read it. However, some might, and anyone that reads your readme will benefit (assuming good info is in the readme and that it's easy to understand).
2. Create a help section in your batch fileIt's as easy as creating a label and liberally using the ECHO command. You could make it so that the user would trigger the help section if they made a syntax error. This section would have ideally the same info as the readme. This is more likely to be seen; however, there will still be a chance of a logic error that will slip through even the best of syntax error catchers.
3. Write code to catch errors in the usage of the batch fileThough you should include at least #2 (it is also recommended that you include #1, it's not hard), this is the most sure-fire way of making sure the user cannot damage his/her system. However, it is also the most involved of the three procedures, and usually will double or triple the file length. This should not, however, concern you. What should concern you is how to write the code in a way that is effective and serves a good purpose.
Let's take a look at an example. A user has an old version of the prompt not equipped with the move.com file that allows you to move files in DOS. This user (Average Joe) has to copy and delete the original. Mr. Joe wants his buddy to write a batch file that allows him to do what newer versions of the prompt can do. Let's say I am that buddy. The easy way out would be to write a batch file with two lines:
copy %1 %2
del %1
But this has its problems as well. What if Joe puts a directory name in, but not the filename! What happens? The file is copied to a
file with the same name as the directory, and the original is erased! Well, no big deal, right? Just rename it to have a .txt or .jpg or whatever extension, right? However, this is inherently inefficient and causes grief as there is no message saying it was copied incorrectly, and it may take time for Joe to figure out he goofed.
The best way to look at writing security for a batch file is to think of all the ways it can go wrong. So, I think about the different ways the copy command can go bad:
- The source does not exist
- The destination directory does not exist
- The destination file already exists
And of course:
- User does not specify source/destination
I. Source does not existEasy enough to check with the following:
IF EXIST %1 GOTO DestCheck
ECHO The file you wish to copy cannot be found. Make sure that you have
ECHO not misspelled your file name and that it does exist.
If the source file exists, it will go to a label marked DestCheck (covered next). The two ECHOs are skipped. However, if the file does not exist, the user is informed of this.
II. Destination directory does not existNot at all hard to do. I check the directory by attempting the copy the user wishes to make:
copy %1 %2\%1
IF ERRORLEVEL 0 GOTO DeleteSource
ECHO The directory you wish to copy to does not exist.
ECHO Please pake sure you have not misspelled the directory name.
This code block will attempt to copy the source to the directory, naming the file the same. (Note: This batch file does not support multiple file copies very well and your readme/help section should note this) If the errorlever (amount of errors) is equal to 0 (no errors), then go to the label that erases the source file; otherwise, notify the user there was an error.
III. User attempts to copy to a file that already existsThis is easy to check, and only slightly more complicated to solve. I find any pre-existing files with the block
IF EXIST %2\%1 GOTO Duplicate
and the label "Duplicate" would have the following code:
ECHO The destination file already exists. The file name
ECHO will be changed to prevent overwrite.
III. User does not specify source/destinationI simply check to see if %1 is empty with this:
IF %1=="" GOTO help
IF %2="" GOTO help
That's it! If %1 is empty, or if %2 is empty, go to the help section.