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 17:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] How to remove quotes from user-passed parameters? DigestI View 13,172 Replies 23
Original Poster Posted 2006-05-26 16:03 ·  中国 北京 联通
初级用户
Credits 124
Posts 34
Joined 2006-05-23 11:38
20-year member
UID 55845
Status Offline
Batch processing gets the user - input parameters, such as
utility "C:\program files\utility",

How to remove the quotes from "C:\program files\utility"?
To do some string concatenation operations, such as

@echo off
@set arg=%1
@set file_path=%arg%\readme.txt
@for /f "usebackq delims=" %%a in (%file_path%) do set a=%%a

Running the above code prompts:
The system cannot find the file C:\Program Files\utility"\version.txt

How to solve it?


───────────────── Moderator's Prompt ─────────────────
The summary of the discussion in this topic is as follows:

  In many cases, we need to remove the possible quotes in a string, and then add our own quotes to make the special characters (command connectors &, |, &&, ||, command line parameter delimiters Space, tab, ;, =, character escape character ^, ", variable escape character %, etc.) character - based, losing their specific functions and becoming an integral part of the string.

  1. There are three simple ways to remove the quotes in the string. Their functions are similar, but their usage occasions are different, and they can handle most situations.

  1 - 1. If the string exists in the command line parameter %1, you can use %~1 to remove the first pair of outer quotes. If there are no outer quotes, the string remains unchanged;

  1 - 2. If the string exists in the for replacement variable %%i, you can use %%~i to remove the first pair of outer quotes. If there are no outer quotes, the string remains unchanged;

  1 - 3. If the string exists in the environment variable %temp%, you can use %temp:"=% to remove all quotes in it. If there are no quotes, the string remains unchanged;

  1 - 4. The above three schemes can be generally used interchangeably to some extent, because as a type of variable, they can be transferred to each other through code or code fragments similar to the following:

1 - 4 - 1. For replacement variable to command line parameter: call:DeQuote %%i
1 - 4 - 2. Environment variable to command line parameter: call:DeQuote %temp%
1 - 4 - 3. Command line parameter to for replacement variable: for %%i in (%1) do...
1 - 4 - 4. Environment variable to for replacement variable: for %%i in (%temp%) do...
1 - 4 - 5. Command line parameter to environment variable: set temp=%1
1 - 4 - 6. For replacement variable to environment variable: for... set temp=%%i

  2. If the quote distribution of the string is very complex, or we have special requirements for the position where the quotes are removed, or there may be some control characters in the string, we can first store the string in the environment variable through the corresponding method in 1 - 4, and then use the following schemes or their combinations for processing:

  2 - 1. You can use set var=%var:~1% to remove the first quote at the beginning of the environment variable var string. If there is no quote at the beginning, the first character is removed;

  2 - 2. You can use set %var:*"=% to remove the first quote at the beginning of the environment variable var string. If there is no quote at the beginning, the variable value remains unchanged;

  2 - 3. You can use set var=%var:~0,-1% to remove the last quote at the end of the environment variable var string. If there is no quote at the end, the last character is removed;

  2 - 4. You can use set "var=%var% to remove the last quote at the end of the environment variable var string. If there is no quote at the end, the environment variable is cleared;

  2 - 5. You can use set var=%var:~1,-1% to remove the outermost pair of quotes of the environment variable var string. If there are no outer quotes, the outer pair of characters is removed;

  2 - 6. You can use %var:*"=set "var=% to remove the outermost pair of quotes of the environment variable var string. If there are no outer quotes, a syntax error occurs;

  2 - 7. You can use set "var=%var:"=%" to remove all possible quotes in the environment variable var string. If there are no outer quotes, the variable value remains unchanged; different from 1 - 3, it allows special control characters to appear in the matching quote pairs of the string;

───────────────── Moderator's Prompt ─────────────────


[ Last edited by willsort on 2006 - 5 - 28 at 22:52 ]
Floor 2 Posted 2006-05-26 17:01 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
@echo off
set arg=%1
echo %arg:"=%
set file=%arg:"=%\1.txt
echo %file%
for /f "usebackq" %%a in ("%file%") do echo %%a

In fact, it's very simple, just didn't think of it.............
Floor 3 Posted 2006-05-27 00:00 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
Can't the expansion feature of CMD variables be used?

set arg=%~1

This can remove the double quotes at the beginning and end.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 4 Posted 2006-05-27 14:57 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Why do I always fail to find the best method? Sad.........Learning.............
Floor 5 Posted 2006-05-27 18:17 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何:

Actually, this problem is more complicated than you and I imagine.

First, Brother bagpipe's code is more suitable for removing quotes from environment variables, while Brother's code is more suitable for removing quotes from command line arguments, each having its own advantages.

Second, no matter which method is used, there is a shortcoming that cannot effectively restrict special characters in the string.

For example, there is a variable test1="C:\Program files", and if Brother's method is used, files will be lost; another example, there is a variable test2="echo test>sample.txt", no matter which method is used, it will cause garbage files unexpectedly generated by the program.

I mentioned this a little in Brother namejm's topic, but it was vaguely stated. Now I bring it up here to discuss it as a topic. Everyone is welcome to give advice.


call:DeQuote "%test1%"

:DeQuote
set "return=%~1"
goto:eof


[ Last edited by willsort on 2006-5-27 at 21:26 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2006-05-27 19:08 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
I'll also throw in a brick:


:Main
set tmpVar=%1
%tmpVar:*"=set "ret=%
goto :Eof


There is a problem with handling multiple quotes in the string, but you can first convert the quotes except for the opening and closing ones in the string, and then convert them back. Just throwing in a brick, hoping to bring out the jade from willsort and Wunaike.
Floor 7 Posted 2006-05-27 19:28 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
TO willsort
There are some problems with the call in brother's code. When the variable value contains quotes and is passed as a parameter, the parameter should not be enclosed in quotes. The potential problem is that it is not easy to determine whether there are quotes in the variable value, so my habitual practice is to use %~n to remove the quotes and then add quotes.



  1. @echo off
  2. set test1="C:\Program files"
  3. set test2="echo test>sample.txt"
  4. call :DeQuote1 %test1%
  5. call :DeQuote2 %test2%
  6. goto:eof

  7. :DeQuote1
  8. set return=%~1
  9. echo %return%
  10. goto:eof

  11. :DeQuote2
  12. set return="star %~1 end"
  13. echo %return%
  14. goto:eof
-=Code coloring BY: Helpless=-
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 8 Posted 2006-05-27 22:06 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何:

What I mean is that when we can't predict whether the string values of test1/test2 contain quotes, we can't use specific methods to constrain special characters in them. For example, the source variable in brother namejm's code is obtained through set /p, and we can't be sure whether the user executing the code has used quotes or will inadvertently cause trouble for our code.

That is to say, when the string value (not just the environment variable) contains quotes, when we reference it in various command lines, we can't use quotes anymore, and when the string value doesn't contain quotes, we need to use quotes for constraint, and this doesn't even consider the situation of two or more pairs of quotes or an odd number of quotes. And because we can't know the content of the string value in advance, we need to detect it in advance, but any detection known so far needs to reference the string value. We need to detect before referencing, and we must reference when detecting, which forms a paradox.

It seems that this problem actually comes from the fact that CMD matches quotes by near matching instead of far matching. I want to know if there is a relatively unconventional way to solve this problem.

Re 3742668:

Your line "%tmpVar:*"=set "ret=% is really wonderful. I'm not very clear about the use of the asterisk here. Can you explain it?

Of course, it also needs to determine in advance that there are quotes outside the string value, otherwise there will be a syntax error.

Re All:

In the current test, set "ret=%test% will remove one trailing quote from the string value containing a trailing quote, which is an expected result; but it is unexpected that the string value without quotes will remove the whole thing. I originally thought it would take ret=%test% as part of the environment variable name. Since the variable matching is invalid, it won't make any modification to %ret%, but the result is that %ret% is cleared.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 9 Posted 2006-05-27 22:51 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Well, I'll make a brief introduction to friends who don't know much about the role of the * sign in the set command as per the requirement of brother willsort:
%PATH:str1=str2%
"str1" can start with an asterisk; in this case, "str1" will keep matching from the start of the expanded result to the first occurrence of the remaining part of str1.
So, in this example, the first quotation mark will be matched. If the * sign is not added, then all quotation marks will be matched, and the resulting result will be incorrect.

set var=www.cn-dos.net
echo %var:.=#% rem The output result should be:www#cn-dos#net
echo %var:*.=#% rem The output result should be:www#cn-dos.net
pause>nul
Floor 10 Posted 2006-05-27 23:21 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
Re about resorting

Understood your meaning, can handle replacing all quotes in the string value. The corresponding problem is that quotes must be added for enclosing during transmission.

set test="C:\Pro&gram" files"
set "test=%test:"=%"
call :DeQuote "%test1%"

Re 3742668

Brother's explanation of * is not easy to understand, can be expressed separately.
1、%PATH:str1=str2%
2、%PATH:*str1=str2%
Among them, 1 replaces all str1 in PATH with str2;
2 replaces from the start of PATH to the part of str1 with str2.
Moreover echo %var:*.=#% The output result should be: #cn-dos.net
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 11 Posted 2006-05-27 23:33 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 3742668:

Got it! I've also found relevant information from set /?. It seems I need to review the command-line help document several more times.

That is to say, the role of the asterisk here is still similar to the wildcard function in file names. It can match 0 to multiple arbitrary characters. However, similar single-character wildcards like? are not yet supported, and the position of the asterisk is only limited to the beginning of the string, not in the middle or at the end of the string.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 12 Posted 2006-05-27 23:37 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Well, thank you Nai Wunaizhi for pointing out the problem. Regarding the explanation of *, it can't be blamed on me. It should be blamed on Microsoft's Help and Support which is not very easy to understand. Being impatient, I'm always a step away from mastery, heh heh.
Floor 13 Posted 2006-05-27 23:42 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
TMD, the Microsoft command explanation is just like a tongue twister (I guess they themselves don't even understand exactly how to put it), but the example from brother 3742668 is much simpler and easier to understand. To put it simply, if you add an asterisk before str1, then it will only replace the first occurrence of str1 in the environment variable, and the others won't be replaced. Is that the meaning?
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 14 Posted 2006-05-28 00:49 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Ups:

An interesting discovery. Due to dense replies, floors 10 to 13 have all become spaced replies. I don't know whether it's due to forum synchronization delay or user refresh delay.

Also, I don't quite understand the reason why Brother Wunaike set "test=%test:"=%" and then called :DeQuote "%test1%".

Finally, I'll make a brief summary on this topic and ask everyone to correct it:

In many cases, we need to remove possible quotes in a string, and then add our own quotes to characterize special characters (command connectors &, |, &&, ||, command line parameter delimiters Space, tab, ;, =, character escape character ^, ", variable escape character %, etc.) so that they lose their specific functions and become ordinary characters as part of the string.

一、There are three simple ways to remove quotes from a string. Their functions are similar, but they are used in different occasions and can handle most situations.

1-1. If the string exists in command line parameter %1, you can use %~1 to remove the first pair of outer quotes. If there are no outer quotes, the string remains unchanged;

1-2. If the string exists in for replacement variable %%i, you can use %%~i to remove the first pair of outer quotes. If there are no outer quotes, the string remains unchanged;

1-3. If the string exists in environment variable %temp%, you can use %temp:"=% to remove all quotes in it. If there are no quotes, the string remains unchanged;

1-4. The above three schemes can be used interchangeably to some extent. Because they are a type of variable, they can be transferred to each other through code or code snippets like the following:
1-4-1. for replacement variable to command line parameter: call:DeQuote %%i
1-4-2. environment variable to command line parameter: call:DeQuote %temp%
1-4-3. command line parameter to for replacement variable: for %%i in (%1) do ...
1-4-4. environment variable to for replacement variable: for %%i in (%temp%) do ...
1-4-5. command line parameter to environment variable: set temp=%1
1-4-6. for replacement variable to environment variable: for ... set temp=%%i

二、If the quote distribution of the string is very complex, or we have special requirements for the position where quotes are removed, or there may be some control characters in the string, you can use the following schemes:

2-1. You can use %test:*"=% to remove the first quote at the beginning of environment variable test string. If there is no quote at the beginning, the variable value remains unchanged;

2-2. You can use set "test=%test% to remove the last quote at the end of environment variable test string. If there is no quote at the end, the variable value is cleared;

2-3. You can use %test:*"=set "test=% to remove the outermost pair of quotes of environment variable test string. If there are no quotes outside, a syntax error occurs;

2-4. You can use set "test=%test:"=%" to remove all possible quotes in environment variable test string. If there are no quotes outside, the variable value remains unchanged; different from 1-3, it can tolerate special control characters in the matching quote pairs of the string.

[ Last edited by willsort on 2006-5-28 at 01:22 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2006-05-28 01:05 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
Re: rewsort

The summary is very detailed, and the wisdom of the masses is infinite ^_^.

Regarding call: DeQuote "%test1%" just wants to illustrate that when the string value contains special characters, quotes must be added during the passing and calling process to make it lose its special nature. Otherwise, it will be interpreted as multiple parameters or multiple commands.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Forum Jump: