In many cases, we need to remove the quotation marks that might exist in a string, and then add our own quotes to make the special characters (Command connector &, | , &&, | |, command line parameter definition space, tab,; , =, the character escape character ^, ", the variable escape character%, etc." characters, lose a specific role, and as ordinary characters become a part of the string.
First, there are three simple ways to remove the quotes in a string, they are similar in function, but they are different in their use and can handle most situations.
1-1. If the string exists in command line parameter%1, you can use%~1 to remove the first external quotation mark if the string does not change without the outside quotation marks;
1-2, if the string exists in the for substitution variable%%i, you can use%%~i to remove the first external quotation mark, if there is no outside quotation mark the string does not change;
1-3, if the string exists in the environment variable%temp%, you can use%temp: "=% off all of the quotes, if there is no quotation mark the string unchanged;"
1-4. The above three scenarios can be used to a certain extent because they are a type of variable that can be transferred to one another by code or code snippets similar to the following:
1-4-1, for substitution variable switch command line arguments: call:dequote%%i 1-4-2, environment variable switch command-line arguments: Call:dequote%temp% 1-4-3, command-line arguments for substitution variable: for%%i in (% 1) Do ... 1-4-4, environment variable for substitution variable: for%%i in (%temp%) do ... 1-4-5, command-line parameter to environment variable: set temp=%1 1-4-6, for substitution variable to environment variable: for ... set temp=%%i
Second, if the string quotation marks are complex, or if we have special requirements for the position of the caret, or some control characters may appear in the string, the string can be transferred first through the corresponding method in 1-4 to the environment variable and processed using the following scenario or its combination:
2-1. You can use set var=%var:~1% to remove the first quotation mark of the environment variable var string, and the first character is removed if there is no quotation mark in the string first;
2-2, you can use the set%var:* "=% off the environment variable var string first quotation mark, if the string does not exist in quotes, the variable value is unchanged;"
2-3. You can use set var=%var:~0,-1% to remove the last quotation mark in the end of the environment variable var string, if the string end does not exist, the last one is removed;
2-4, you can use set "var=%var% off the environment variable var string end of the last quotation mark, if the string end does not exist in quotes, the environment variable is emptied;"
2-5, you can use set var=%var:~1,-1% off the environment variable VAR string the most outer pair of quotes, if the string outside the side does not exist in quotes then the outer side of a pair of characters are removed;
2-6, you can use the%var:* "=set" var=% off the environment variable VAR string the most outer pair of quotes, if there is no string outside the quotation marks will appear grammatical errors;
2-7, you can use the set "Var=%var:" "=%" remove all possible quotes in the environment variable var string, if the string does not appear in quotes, the variable value is unchanged; unlike 1-3, it allows string matching quotes to appear inside special control characters;