中国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-06 20:11
47,812 topics / 349,914 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » [fixed] Why does the variable escape character %% only display as one %
Printable Version  7,051 / 11
Floor1 bagpipe Posted 2006-02-11 10:55
银牌会员 Posts 425 Credits 1,144 From 北京
Using the ECHO command to display " %~dpI" is the issue. Why is it that when writing a batch file, you have to use the statement ECHO %%~dpI in order to display " %~dpI"? Why does it show only one when using the echo command? I couldn't find any materials about this, so I'd like to ask brothers WILLSORT and 无奈何 to help solve it. Thanks in advance.

[ Last edited by willsort on 2006-6-8 at 12:34 ]
Floor2 JonePeng Posted 2006-02-11 15:15
金牌会员 Posts 1,883 Credits 4,562 From 广东广州
On the command line you use one %, in a batch file you use two %. It's not just the echo command; even for the %i in the for command has to be written as %%i in a batch file.
Floor3 bagpipe Posted 2006-02-12 08:55
银牌会员 Posts 425 Credits 1,144 From 北京
I think brother JonePeng misunderstood what I meant, hehe. In a batch file the FOR statement does have to use the form %%I, but I'd better give an example. First, thanks to brother JonePeng for being willing to help me. Now look at this example:

@echo off
echo %~f0
echo %%~f0

In this batch file, the result produced by the first ECHO is the full path of this batch file, while the second ECHO displays the characters "%~f0". What I don't understand now is, why do the two %% become one % when displayed with ECHO???
Floor4 willsort Posted 2006-02-12 09:21
元老会员 Posts 1,512 Credits 4,432

───────────────── Moderator note ─────────────────
For the convenience of forum users browsing and moderators managing, please modify the title of this topic so that it briefly describes the content or intent of the topic
This topic is better posted in "Q&A Room", because this topic is closer in classification to questions and answers
───────────────── Moderator note ─────────────────

Re bagpipe:

Your question involves two factors:

1. % is an ESCAPE character. It is usually translated as an escape character, but there are also more vivid names like disengage character, escape-away character, etc. That is to say, % not only escapes and replaces the specific string associated with it with a specific string, but it itself is also "escaped away." And similar to the escape character "\" in C language, double % will escape and reduce to a single %, while four % will reduce to two %.

2. for itself is a special command, somewhat like a specialized command interpreter, because implementing its function requires executing multiple statements, so it must also have the ability to parse and process the command line (specifically the command line after do). So when command/cmd implements for, it naturally reuses its own original command-line parsing module, therefore for has the characteristic of second-level escaping. The statement after do in for is parsed and interpreted in two stages: the first stage is when command/cmd reads in and interprets the for command line, the second stage is when for reads in and interprets the do command, and it will usually interpret the same command line multiple times.

Then we can note that when using command-line parameter variables and environment variables in do, double % is not needed. That is because after passing through the first-level escape process, these variables are replaced with specific immutable string constants, which participate in all execution processes of the for loop; but replacement variables are required to change dynamically during execution (in the sub-command line after do), and this change naturally still needs to be implemented through the escape character, so using double % becomes the inevitable choice.

In addition, it should also be noted that when using for on the command line, double % is not needed. This comes from the fact that the command interpreter handles the command line and batch processing differently. In early DOS versions, % was not regarded as an escape character on the command line, so it would not be escaped away, and therefore environment variables could not be directly referenced on the command line at that time. When using for, only one % is needed for for to perform escaping and reduction. In later versions of the command interpreter, support for command-line escaping was added (mainly support for environment variables), but the tradition of using a single % with command-line for was still preserved.

And delayed variable substitution in cmd is a special case, but it does not violate the escaping principle above; it is just that the environment variables in for are no longer constants.

[ Last edited by willsort on 2006-2-12 at 09:57 ]
Floor5 bagpipe Posted 2006-02-12 12:19
银牌会员 Posts 425 Credits 1,144 From 北京
Thanks to moderator WILLSORT for the reply. I seem to understand it a bit now. Thanks to both of you for your help. It's best when everyone works together. Thanks again!
Floor6 jun19841 Posted 2006-06-01 01:19
初级用户 Posts 29 Credits 55 From 亚洲
Wow · ···································
^Ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha ^ha ha
Floor7 chineselgs Posted 2006-06-04 16:25
高级用户 Posts 266 Credits 613 From 河南省
Is the one above a madman??
Floor8 不得不爱 Posted 2006-06-06 09:47
超级版主 Posts 2,044 Credits 5,310 From 四川南充
───────────────── Moderation record ─────────────────
Executed by: qwe1234567
Action: Move topic: from DOS Learning Basics & Featured Articles (Tutorial Room)
Explanation: According to willsort's suggestion and based on the topic content classification, it is more suitable to be posted in this section
Penalty: Not for now!
───────────────── Moderation record ─────────────────
Floor9 willsort Posted 2006-06-08 12:32
元老会员 Posts 1,512 Credits 4,432

───────────────── Moderation record ─────────────────
Executed by: Will Sort
Action: Modified title - I have a question, please ask moderator willsort to help solve it, thanks
Explanation: Three days after the notice was issued, the original topic author still did not appeal or modify it, so the moderator modified it and imposed a penalty
Penalty: Deduct the 6 credits awarded for posting this topic, and deduct an additional punitive 2 credits for violating title rules
───────────────── Moderation record ─────────────────



───────────────── Moderation record ─────────────────
Executed by: Will Sort
Action: Move topic: from "DOS Troubleshooting & Question Discussion (Q&A Room)"
Explanation: This topic conforms to the sectioning principles of the newly established board area, and the discussion has temporarily come to an end
───────────────── Moderation record ─────────────────
Floor10 zhangxue Posted 2006-06-08 14:37
初级用户 Posts 82 Credits 183
Question for (under the ms-dos environment):
If
%a%=b
%b%=2
then how do you associate a with 2? I found that %%a%% is not equal to 2
Floor11 kcdsw Posted 2006-06-09 09:38
中级用户 Posts 179 Credits 404
Floor12 mysuntjy Posted 2008-12-23 22:57
新手上路 Posts 10 Credits 13 From 重庆市
Originally posted by zhangxue at 2006-6-8 14:37:
Question for (under the ms-dos environment):
If
%a%=b
%b%=2
then how do you associate a with 2? I found that %%a%% is not equal to 2

Use this on the command line: call, echo %%a%% is enough.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023