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 ]
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 ]

DigestI