中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-11-10 12:41
47,815 topics / 349,930 posts / today 0 new / 48,291 members
DOS批处理 & 脚本技术(批处理室) » [Discussion] How to remove quotes from user-passed parameters?
Printable Version  14,105 / 23
Floor16 3742668 Posted 2006-05-28 01:49
荣誉版主 Posts 718 Credits 2,013
Well, it turns out that the final summary work is done by willsort. Only the rigorous attitudes of willsort and Wunaaihe are suitable for posting this theoretical post. But, I'm still showing off my skills, showing off in front of Guan Yunchang:
2.5. You can use %var:~1% to remove the leading quote.
2.6. You can use %var:~1,-1% to remove both leading and trailing quotes
2.7. You can use %var:~0,-1% to remove the trailing quote.

[ Last edited by 3742668 on 2006-5-28 at 02:02 ]
Floor17 Climbing Posted 2006-05-28 02:50
铂金会员 Posts 2,753 Credits 6,964 From 河北保定
All three of you are great experts, and I'm just here to post casually. I've been studying hard!
Floor18 willsort Posted 2006-05-28 23:42
元老会员 Posts 1,512 Credits 4,432
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?
Floor19 3742668 Posted 2006-05-29 13:24
荣誉版主 Posts 718 Credits 2,013
It's a long post. In my habit, generally, I try to avoid using the if statement as much as possible and instead use commands like echo, dir, etc. to operate on variables and perform corresponding operations based on the error return values. Even when using the format of if "%~1"==, I think that if %1 contains strings like "==" that are unpredictable and similar to the structure of the if statement (a bit of a nitpick?), there will inevitably be errors (I haven't tested it, but it's just an idea from a long time ago, which led to my habit all along). For the if statement, maybe not using it when possible is the best approach. Of course, this is just my humble opinion. After all, I just dabbled in it and didn't delve deeply. I have seen the official theory of willsort, looking forward to the insights of Wunaiaohe, heh heh.
ps: This post has been highlighted as an essence post. All readers, hurry up and leave a mark. This is the first time I've mixed into the legendary essence post when coming to cn-dos.
Floor20 tclgb Posted 2007-07-08 17:24
初级用户 Posts 26 Credits 76
Read it three times repeatedly, good
Floor21 rockdong Posted 2007-08-23 14:28
初级用户 Posts 25 Credits 48
It's very useful, thank you!
Floor22 upsco Posted 2007-11-19 20:46
初级用户 Posts 32 Credits 67
2-4. You can use set "var=%var% to remove the last quote at the end of the string of environment variable var. If there is no quote at the end, the environment variable is cleared;

Is it because of the version? In XP, if there is no quote in var, the set "var=%var% value remains unchanged
Floor23 upsco Posted 2007-11-19 21:16
初级用户 Posts 32 Credits 67
Original text contains Chinese and the content is about some logic description. Since it's not a complete English sentence but rather a Chinese - structured description, the translation would be:

Original Chinese content: 原来set "var=%var%,如果var
1、不含引号,数值不变
2、如果有引号,且不在首位,保留倒数第一个引号前的值
3、如果有引号都在串首,清空变量

Translated content: Originally set "var=%var%, if var
1. If there are no quotes, the value remains unchanged
2. If there are quotes and they are not at the first position, keep the value before the last occurrence of the quote
3. If all quotes are at the beginning of the string, clear the variable
Floor24 laihaibin08 Posted 2008-10-19 17:53
新手上路 Posts 15 Credits 13
A bit too deep...
Prev  1 2
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023