BAT parameters go to quotes (all sorts of exotic ways of quoting) _dos/bat

Source: Internet
Author: User
Tags control characters sorts

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.