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-22 05:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [fixed] Why does the variable escape character %% only display as one % DigestI View 6,847 Replies 11
Original Poster Posted 2006-02-11 10:55 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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 ]
Floor 2 Posted 2006-02-11 15:15 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
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.
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 3 Posted 2006-02-12 08:55 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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???
Floor 4 Posted 2006-02-12 09:21 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── 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 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2006-02-12 12:19 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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!
Floor 6 Posted 2006-06-01 01:19 ·  中国 江西 南昌 电信
初级用户
Credits 55
Posts 29
Joined 2006-04-11 16:40
20-year member
UID 53669
Gender Male
From 亚洲
Status Offline
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
Floor 7 Posted 2006-06-04 16:25 ·  中国 福建 泉州 电信
高级用户
★★
论坛灌水专业户
Credits 613
Posts 266
Joined 2006-04-19 22:47
20-year member
UID 54113
From 河南省
Status Offline
Is the one above a madman??
饮马恒河畔,剑指天山西,碎叶城揽月,库叶岛赏雪,黑海之滨垂钓,贝尔加湖面张弓;中南半岛访古,东京废墟祭祖!
Floor 8 Posted 2006-06-06 09:47 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
───────────────── 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 ─────────────────
Floor 9 Posted 2006-06-08 12:32 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── 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 ─────────────────
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2006-06-08 14:37 ·  中国 广东 清远 联通
初级用户
★★
Credits 183
Posts 82
Joined 2006-03-28 21:18
20-year member
UID 52959
Status Offline
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
Floor 11 Posted 2006-06-09 09:38 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Floor 12 Posted 2008-12-23 22:57 ·  中国 重庆 沙坪坝区 电信
新手上路
Credits 13
Posts 10
Joined 2008-11-13 00:09
17-year member
UID 130805
Gender Male
From 重庆市
Status Offline
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.
Forum Jump: