先声明一下以下文章,是个人查找一些相关资料后自己总结出来的,由于自己刚学dos不久,所以文中的观点并不全正确,希望大家能够希望大家能够不吝赐教。 by-無名氏
先来说一下我cmd的当前目录中放有
- ab.txt(里面写着file:ab.txt)
- a b.txt(里面写着b.txtfile:a (space) b.txt)
看的时候大家注意IN 后面括号中的文字。
首先我们来看看,for中不采用usebackq时的情况。
实验一:
@FOR /F "tokens=*" %i IN (ab.txt) DO @ECHO %i
结果:
file:ab.txt
实验二:
@FOR /F "tokens=*" %i IN (a b.txt) DO @ECHO %i
结果:
系统找不到文件 a。
总结:(IN后面的,下同)括号中的文字不加东西(这里的东西指双引号,单引号,还有back quoted。关于back quoted我将在下面说明)时,指的是文件,但文件中不能有空格,否则就会出错。
实验三:
@FOR /F "tokens=*" %i IN ('ECHO Hello') DO @ECHO %i
结果:
Hello
总结:这里结果是hello表示这里的ECHO应该是当作一个命令来执行的,即把ECHO Hello的结果传给%i
实验四:
@FOR /F "tokens=*" %i IN ("ECHO Hello") DO @ECHO %i
结果:
ECHO Hello
总结:这个结果的ECHO Hello,表明这里的echo 不是当作dos中的命令来处理,而是当作字符来处理。
实验五:
@FOR /F "tokens=*" %i IN (`ECHO Hello`) DO @ECHO %i
结果:
系统找不到文件 `ECHO。
说明关于这个例子,你也许会认为和第二个相同,如果你仔细看的话会发现有一点不同,这个用的是back quoted。你可以先暂时跳过这个,等看完下面的在回来看这个。
下面看看使用usebackq的效果:
在做下面实验以前先做一些准备工作,首先我把cmd /?中关于usebackq部分拷过来。
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.
看到这个帮助,我认为这里讲了usebackq的三种用法,单引号,双引号,back quote.在下面的实验中我会一个一个来讲的。(注意区分单引号和back quote)
实验六:
@FOR /F "usebackq tokens=*" %i IN (ab.txt) DO @ECHO %i
结果:
file:ab.txt
实验七:
@FOR /F "usebackq tokens=*" %i IN (a b.txt) DO @ECHO %i
结果:
系统找不到文件 a。
总结:括号中文字不加东西时,与不使用usebackq的作用是相同(参考实验一,实验二):即都表示的是文件,但文件中不能有空格,否则就会出错。
到了这里也许会有人想,如果我的文件里面有空格该怎么办呢?下面的这个实验,就会帮你解决这个问题。
实验八:
@FOR /F "usebackq tokens=*" %i IN ("a b.txt") DO @ECHO %i
结果:
file:a (space) b.txt
总结:当使用usebackq这个关键字之后,双引号中的文字不在代表字符串(参见例四),而且代表文件名了。即使文件中有空格也可以使用。
那么当使用usebackq关键字之后,但又想使用字符串,要该么办,其实这时是用单引号来代表字符串。看下面的实验。
@FOR /F "usebackq tokens=*" %i IN ('ECHO hello') DO @ECHO %i
结果:
ECHO hello
总结:这个和实验四的结果一样,证明了,单引号里面的是字符串。
实验九:
@FOR /F "usebackq tokens=*" %i IN (`ECHO hello`) DO @ECHO %i
结果:
hello
注意了这里不是单引号,而是back quote,这个结果说明,当使用back quote时里面的是命令。也就相当于不使用usebackq关键字的单引号。当然,如果你可以把ECHO hello其它dos命令,把执行的结果传给%i,如果你输入的不是dos命令,比如abc Hello,cmd会提示你abc不是命令。
Last edited by tianlijian on 2007-3-16 at 08:33 PM ]
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:
- 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 ]