First, I would like to declare that the following article is a summary I made after finding some relevant information. Since I have just started learning DOS, the views in the article are not all correct. I hope everyone can not be stingy with giving advice.
by - Anonymous
First, let's talk about the current directory in my cmd where there are:
When reading, everyone should pay attention to the text in the parentheses after IN.
First, let's take a look at the situation when usebackq is not used in for.
Experiment 1:
Result: file:ab.txt
Experiment 2:
Result: The system cannot find the file a.
Summary: (The same below after IN) When there is no addition in the parentheses (here, the addition refers to double quotes, single quotes, and back quoted. I will explain back quoted below), it refers to a file, but if there is a space in the file, it will be wrong.
Experiment 3:
Result: Hello
Summary: The result here is hello, which means that ECHO should be regarded as a command here, that is, the result of ECHO Hello is passed to %i.
Experiment 4:
Result: ECHO Hello
Summary: The result of this ECHO Hello shows that echo here is not treated as a command in DOS, but as a character.
Experiment 5:
Result: The system cannot find the file `ECHO.
Explanation: For this example, you may think it is the same as the second one. If you look carefully, you will find a little difference. This one uses back quoted. You can skip this for now and come back to it after reading below.
Now let's see the effect of using usebackq:
Before doing the following experiments, first I copied the part about usebackq in cmd /?.
Seeing this help, I think there are three usages of usebackq here: single quotes, double quotes, back quote. I will talk about them one by one in the following experiments. (Note to distinguish between single quotes and back quote)
Experiment 6:
Result: file:ab.txt
Experiment 7:
Result: The system cannot find the file a.
Summary: When there is no addition in the parentheses, it is the same as not using usebackq (refer to Experiment 1, Experiment 2): both represent a file, but if there is a space in the file, it will be wrong.
Up to here, some people may think, if there is a space in my file, what should I do? The following experiment will help you solve this problem.
Experiment 8:
Result: file:a (space) b.txt
Summary: After using the usebackq keyword, the text in double quotes does not represent a string (refer to Example 4) and represents a file name. Even if there is a space in the file, it can be used.
Then, after using the usebackq keyword, but if you want to use a string, what should you do? In fact, at this time, single quotes are used to represent the string. Look at the following experiment.
Result: ECHO hello
Summary: This is the same as the result of Experiment 4, which proves that the content in single quotes is a string.
Experiment 9:
Result: hello
Note that this is not a single quote, but a back quote. This result shows that when using back quote, the content inside is a command. It is equivalent to the single quote without using the usebackq keyword. Of course, if you can put other DOS commands in ECHO hello, pass the execution result to %i. If you enter something that is not a DOS command, such as abc Hello, cmd will prompt you that abc is not a command.
[ Last edited by tianlijian on 2007-3-16 at 08:33 PM ]
by - Anonymous
First, let's talk about the current directory in my cmd where there are:
- ab.txt (which says file:ab.txt)
- a b.txt (which says b.txtfile:a (space) b.txt)
When reading, everyone should pay attention to the text in the parentheses after IN.
First, let's take a look at the situation when usebackq is not used in for.
Experiment 1:
@FOR /F "tokens=*" %i IN (ab.txt) DO @ECHO %i Result: file:ab.txt
Experiment 2:
@FOR /F "tokens=*" %i IN (a b.txt) DO @ECHO %i Result: The system cannot find the file a.
Summary: (The same below after IN) When there is no addition in the parentheses (here, the addition refers to double quotes, single quotes, and back quoted. I will explain back quoted below), it refers to a file, but if there is a space in the file, it will be wrong.
Experiment 3:
@FOR /F "tokens=*" %i IN ('ECHO Hello') DO @ECHO %i Result: Hello
Summary: The result here is hello, which means that ECHO should be regarded as a command here, that is, the result of ECHO Hello is passed to %i.
Experiment 4:
@FOR /F "tokens=*" %i IN ("ECHO Hello") DO @ECHO %i Result: ECHO Hello
Summary: The result of this ECHO Hello shows that echo here is not treated as a command in DOS, but as a character.
Experiment 5:
@FOR /F "tokens=*" %i IN (`ECHO Hello`) DO @ECHO %i Result: The system cannot find the file `ECHO.
Explanation: For this example, you may think it is the same as the second one. If you look carefully, you will find a little difference. This one uses back quoted. You can skip this for now and come back to it after reading below.
Now let's see the effect of using usebackq:
Before doing the following experiments, first I copied the part about usebackq in cmd /?.
specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
filenameset.
Seeing this help, I think there are three usages of usebackq here: single quotes, double quotes, back quote. I will talk about them one by one in the following experiments. (Note to distinguish between single quotes and back quote)
Experiment 6:
@FOR /F "usebackq tokens=*" %i IN (ab.txt) DO @ECHO %i Result: file:ab.txt
Experiment 7:
@FOR /F "usebackq tokens=*" %i IN (a b.txt) DO @ECHO %i Result: The system cannot find the file a.
Summary: When there is no addition in the parentheses, it is the same as not using usebackq (refer to Experiment 1, Experiment 2): both represent a file, but if there is a space in the file, it will be wrong.
Up to here, some people may think, if there is a space in my file, what should I do? The following experiment will help you solve this problem.
Experiment 8:
@FOR /F "usebackq tokens=*" %i IN ("a b.txt") DO @ECHO %i Result: file:a (space) b.txt
Summary: After using the usebackq keyword, the text in double quotes does not represent a string (refer to Example 4) and represents a file name. Even if there is a space in the file, it can be used.
Then, after using the usebackq keyword, but if you want to use a string, what should you do? In fact, at this time, single quotes are used to represent the string. Look at the following experiment.
@FOR /F "usebackq tokens=*" %i IN ('ECHO hello') DO @ECHO %i Result: ECHO hello
Summary: This is the same as the result of Experiment 4, which proves that the content in single quotes is a string.
Experiment 9:
@FOR /F "usebackq tokens=*" %i IN (`ECHO hello`) DO @ECHO %i Result: hello
Note that this is not a single quote, but a back quote. This result shows that when using back quote, the content inside is a command. It is equivalent to the single quote without using the usebackq keyword. Of course, if you can put other DOS commands in ECHO hello, pass the execution result to %i. If you enter something that is not a DOS command, such as abc Hello, cmd will prompt you that abc is not a command.
[ Last edited by tianlijian on 2007-3-16 at 08:33 PM ]
Recent Ratings for This Post
( 1 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| sxhzx | +1 | 2008-12-28 19:38 |

