中国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-09-14 17:22
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » [Discussion] How to remove quotes from user-passed parameters?
Printable Version  13,608 / 23
Floor1 wingofsea Posted 2006-05-26 16:03
初级用户 Posts 34 Credits 124
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 ]
Floor2 bagpipe Posted 2006-05-26 17:01
银牌会员 Posts 425 Credits 1,144 From 北京
@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.............
Floor3 无奈何 Posted 2006-05-27 00:00
荣誉版主 Posts 356 Credits 1,338
Can't the expansion feature of CMD variables be used?

set arg=%~1

This can remove the double quotes at the beginning and end.
Floor4 bagpipe Posted 2006-05-27 14:57
银牌会员 Posts 425 Credits 1,144 From 北京
Why do I always fail to find the best method? Sad.........Learning.............
Floor5 willsort Posted 2006-05-27 18:17
元老会员 Posts 1,512 Credits 4,432
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.



[ Last edited by willsort on 2006-5-27 at 21:26 ]
Floor6 3742668 Posted 2006-05-27 19:08
荣誉版主 Posts 718 Credits 2,013
I'll also throw in a brick:



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.
Floor7 无奈何 Posted 2006-05-27 19:28
荣誉版主 Posts 356 Credits 1,338
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=-
Floor8 willsort Posted 2006-05-27 22:06
元老会员 Posts 1,512 Credits 4,432
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.
Floor9 3742668 Posted 2006-05-27 22:51
荣誉版主 Posts 718 Credits 2,013
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.
Floor10 无奈何 Posted 2006-05-27 23:21
荣誉版主 Posts 356 Credits 1,338
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
Floor11 willsort Posted 2006-05-27 23:33
元老会员 Posts 1,512 Credits 4,432
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.
Floor12 3742668 Posted 2006-05-27 23:37
荣誉版主 Posts 718 Credits 2,013
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.
Floor13 Climbing Posted 2006-05-27 23:42
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
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?
Floor14 willsort Posted 2006-05-28 00:49
元老会员 Posts 1,512 Credits 4,432
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 ]
Floor15 无奈何 Posted 2006-05-28 01:05
荣誉版主 Posts 356 Credits 1,338
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023