Re 3742668:
A new summary has been edited into the top post for easy browsing, and your suggestions have been incorporated into 2-1, 2-3, and 2-5 respectively.
As Brother Wu Naihe said, the wisdom of the collective is far greater than the sum of the wisdom of individuals. Now another topic related to quotes is thought of, and everyone is invited to discuss it.
Defensive Errors in Command Line Arguments
In my past code, when comparing command line arguments in an if statement, I always liked to use a format like if "%1"=="string1" ... Now, thinking about it, this habit has quite some problems.
Because the purpose of using quotes is to defend against possible escape characters in the string. However, after the command line parsing by CMD, all possible escape characters that could be escaped should have already been processed and removed, and the remaining escape characters should have been pre-defended in the command line. Therefore, %1 cannot contain spaces or other escape characters without being defended by quotes, and thus using quotes outside %1 is redundant.
Further speaking, if %1 is a string that has been defended by quotes, such as "C:\Program files", then using "%1" to reference it becomes ""C:\Program files"", the quotes are re-matched, and thus the escape characters are exposed, causing the program to go wrong. Therefore, using quotes at this time is wrong.
Then, do we not need to defend %1 in the if statement? The answer is no. Because the defense character in if was originally to defend against %1 being empty, like the format if "%1"=="" ... So I once called the defense character an "empty defense character". If we do not defend %1, then when the batch processing is executed without parameters, a syntax error will occur at this sentence.
At this point, our best choice is to remove the quotes from %1 and then use quotes for defense. For example, if "%~1"=="C:\Program file" ... or if exist "%~1" .... This is indeed a very good way.
However, the implementation of removing quotes is relatively simple only in CMD. And many of my batch processes still run under 9x and DOS, so I need to fall back to think of other methods. For example, select other characters as the defense characters for the command line argument string. There are many optional defense characters in the if statement for string comparison. Almost most non-escape characters can be used for defense. For example, the if == ... that I used early.
However, it also needs to be considered that the defense character will also appear in other statements, such as the file existence judgment statement if exist. If we use the paradigm if exist ..., then the process of the program will obviously deviate from our expected one. Although my usual practice is to have if == ... and then if exist %1 ... .
Therefore, in order to maintain the readability of the code, I need to use a consistent defense character, so our remaining choices are few. Perhaps, we can use a trailing period. For example, if %1.==. ... or if exist %1. ...
It can solve the partial defense problem when the string is a file name, but when the string is a path . or .., if exist %1. will obviously go wrong.
So is there a better solution?
※ Batchinger 致 Bat Fans:请访问
批处理编程的异类 ,欢迎交流与共享批处理编程心得!