China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 16:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] How to remove quotes from user-passed parameters? DigestI View 13,169 Replies 23
Floor 16 Posted 2006-05-28 01:49 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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 ]
Floor 17 Posted 2006-05-28 02:50 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
All three of you are great experts, and I'm just here to post casually. I've been studying hard!
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 18 Posted 2006-05-28 23:42 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 19 Posted 2006-05-29 13:24 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Floor 20 Posted 2007-07-08 17:24 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
Read it three times repeatedly, good
Floor 21 Posted 2007-08-23 14:28 ·  中国 江苏 苏州 电信
初级用户
Credits 48
Posts 25
Joined 2007-07-30 17:57
19-year member
UID 94318
Gender Male
Status Offline
It's very useful, thank you!
Floor 22 Posted 2007-11-19 20:46 ·  中国 湖南 长沙 雨花区 电信
初级用户
Credits 67
Posts 32
Joined 2007-11-19 12:34
18-year member
UID 103118
Gender Male
Status Offline
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
Floor 23 Posted 2007-11-19 21:16 ·  中国 湖南 长沙 雨花区 电信
初级用户
Credits 67
Posts 32
Joined 2007-11-19 12:34
18-year member
UID 103118
Gender Male
Status Offline
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
Floor 24 Posted 2008-10-19 17:53 ·  中国 江西 赣州 电信
新手上路
Credits 13
Posts 15
Joined 2008-10-19 15:37
17-year member
UID 128763
Gender Male
Status Offline
A bit too deep...
Forum Jump: