China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-08 22:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Self-summary of the usage of usebackq in FOR /F View 3,544 Replies 18
Original Poster Posted 2007-03-17 04:37 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
sxhzx +1 2008-12-28 19:38
Floor 2 Posted 2007-03-17 04:41 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
Reserved post, a more concise summary will be given later

[ Last edited by tianlijian on 2007-3-16 at 05:29 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +10 2007-03-17 12:08
Attachments
usebackq.png
Floor 3 Posted 2007-03-17 08:06 ·  中国 广西 梧州 电信
新手上路
Credits 2
Posts 1
Joined 2007-02-08 05:27
19-year member
UID 78945
Gender Male
Status Offline
Thank you, the original poster, for making these detailed summaries of the details of FOR. I'm a newbie. I didn't understand the FOR statement. Reading your summary made me understand these details. Thank you again, the original poster.
Floor 4 Posted 2007-03-17 12:11 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
  Well summarized, I'll give you an extra point first.

  Brother, try the following interesting command and see what happens:

for /f "usebackq delims=" %i in ('echo hello') do @echo %i
Floor 5 Posted 2007-03-17 16:05 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
It seems that in some cases the result is this
echo hello

And in some other cases the result is this
echo hello
Ť \。Cannot find the file Ɉ

Don't understand why?

[ Last edited by tianlijian on 2007-3-17 at 03:07 AM ]
Floor 6 Posted 2007-03-17 16:25 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
Tried many times but still don't know why, both situations above will occur, and the first one seems to be more.
I initially thought whether there were invisible characters when using copy to clipboard for copying, but then I thought it wasn't like that.
There is another phenomenon, I found that sometimes when pasting the copied content in cmd and right-clicking, the result will be directly displayed, and sometimes I have to press Enter once?
Floor 7 Posted 2007-03-19 01:00 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Everyone, please paste the strange result you got back into the command line and take a look. I think this is what the computer really wants to say.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 8 Posted 2007-03-19 04:11 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
Still don't understand what it means exactly? Why is there this result sometimes and another result at other times?

[ Last edited by tianlijian on 2007-3-18 at 03:13 PM ]
test
Floor 9 Posted 2007-10-29 22:51 ·  中国 上海 静安区 中移铁通
初级用户
Credits 48
Posts 23
Joined 2007-10-24 09:46
18-year member
UID 100622
Gender Male
Status Offline
Why are all my results under CMD echo hello?
Floor 10 Posted 2007-10-29 22:59 ·  中国 上海 静安区 中移铁通
初级用户
Credits 48
Posts 23
Joined 2007-10-24 09:46
18-year member
UID 100622
Gender Male
Status Offline
Originally posted by tianlijian at 2007-3-19 04:11 AM:
Still don't understand what it means exactly?
Why is there sometimes this result, and sometimes another result?

[ Last edited by tianlijian on 2007-3-18 at 03:13 PM ]

A strange result will be obtained,
Floor 11 Posted 2008-01-30 18:41 ·  中国 河北 沧州 青县 联通
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
Admire the spirit of the original poster.
Floor 12 Posted 2008-03-03 21:00 ·  中国 重庆 电信
初级用户
Credits 34
Posts 14
Joined 2007-12-13 18:21
18-year member
UID 105563
Gender Male
Status Offline
That's a good summary. The moderator's example is strange.
Floor 13 Posted 2008-04-03 16:40 ·  中国 上海 电信数据中心
初级用户
★★
Credits 184
Posts 73
Joined 2007-05-24 18:06
19-year member
UID 89185
Gender Male
Status Offline
```
@echo off
FOR /F "usebackq tokens=*" %%i IN (`"C:\a.bat" 'ab cd'`) DO ECHO %%i
pause
```
c:\a.bat content is @echo _____%1
The output result I expected is
_____ab cd
However, the actual result is
_____'ab
```
Floor 14 Posted 2008-04-03 16:42 ·  中国 上海 电信数据中心
初级用户
★★
Credits 184
Posts 73
Joined 2007-05-24 18:06
19-year member
UID 89185
Gender Male
Status Offline
It seems that there might be an issue with the way the command line arguments are being passed. Let's take a look at the code. The problem is likely related to how the spaces in the argument "ab cd" are being handled. When you pass an argument with spaces in a `FOR /F` command that's executing another batch file, you need to enclose the entire argument in quotes properly. Maybe you should adjust the way you're passing the argument to the called batch file. The error message indicates that the system is trying to interpret "C:\a.bat" "ab" as a command, which isn't correct. So the translated code remains as the original code provided, and the analysis is as above.

But according to the requirements, we just need to provide the translated text. The original code is in English already in terms of the code part, but the description part is translated as above. Wait, no, wait. Wait the user provided the Chinese text "改了一下还是不行" which translates to "Still not working after making changes" and then the code block. So the overall translated output should be:

Still not working after making changes
@echo off
FOR /F "usebackq tokens=*" %%i IN (`"C:\a.bat" "ab cd"`) DO ECHO %%i
pause

Output result is:
'C:\a.bat" "ab' is not recognized as an internal or external command,
operable program or batch file.
Floor 15 Posted 2008-04-03 17:02 ·  中国 广西 防城港 电信
中级用户
★★
海浪人生
Credits 206
Posts 103
Joined 2007-07-04 00:18
19-year member
UID 93068
Gender Male
Status Offline
Hehe, learned a little bit
Forum Jump: