中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-18 04:11
47,812 topics / 349,917 posts / today 0 new / 48,270 members
DOS批处理 & 脚本技术(批处理室) » Self-summary of the usage of usebackq in FOR /F
Printable Version  3,907 / 18
Floor1 tianlijian Posted 2007-03-17 04:37
初级用户 Posts 45 Credits 120
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:

    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 /?.
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:

    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 ]
Floor2 tianlijian Posted 2007-03-17 04:41
初级用户 Posts 45 Credits 120
Reserved post, a more concise summary will be given later

[ Last edited by tianlijian on 2007-3-16 at 05:29 PM ]

Attachments
usebackq.png (12.75 KiB)
Floor3 snk502 Posted 2007-03-17 08:06
新手上路 Posts 1 Credits 2
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.
Floor4 lxmxn Posted 2007-03-17 12:11
版主 Posts 4,938 Credits 11,386
  Well summarized, I'll give you an extra point first.

  Brother, try the following interesting command and see what happens:
Floor5 tianlijian Posted 2007-03-17 16:05
初级用户 Posts 45 Credits 120
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 ]
Floor6 tianlijian Posted 2007-03-17 16:25
初级用户 Posts 45 Credits 120
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?
Floor7 oilio Posted 2007-03-19 01:00
高级用户 Posts 303 Credits 641
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.
Floor8 tianlijian Posted 2007-03-19 04:11
初级用户 Posts 45 Credits 120
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 ]
Floor9 z310394543 Posted 2007-10-29 22:51
初级用户 Posts 23 Credits 48
Why are all my results under CMD echo hello?
Floor10 z310394543 Posted 2007-10-29 22:59
初级用户 Posts 23 Credits 48
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,
Floor11 sglxy Posted 2008-01-30 18:41
中级用户 Posts 87 Credits 397
Admire the spirit of the original poster.
Floor12 h2thesun Posted 2008-03-03 21:00
初级用户 Posts 14 Credits 34
That's a good summary. The moderator's example is strange.
Floor13 sinbao Posted 2008-04-03 16:40
初级用户 Posts 73 Credits 184
```
@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
```
Floor14 sinbao Posted 2008-04-03 16:42
初级用户 Posts 73 Credits 184
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

Output result is:
'C:\a.bat" "ab' is not recognized as an internal or external command,
operable program or batch file.
Floor15 p1509101 Posted 2008-04-03 17:02
中级用户 Posts 103 Credits 206
Hehe, learned a little bit
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023