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-06-20 17:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Who can explain the "%"? View 14,049 Replies 72
Original Poster Posted 2006-05-09 22:55 ·  中国 北京 教育网
初级用户
Credits 84
Posts 28
Joined 2006-05-03 22:36
20-year member
UID 54894
Gender Male
Status Offline
“%” This thing, one or two are understandable
But when there are too many, it's dizzying

Which expert can explain it?
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
ytfy +2 2008-02-19 18:46
+1 2010-07-19 13:02
Floor 2 Posted 2006-05-10 01:47 ·  中国 广西 河池 金城江区 电信
初级用户
Credits 80
Posts 29
Joined 2006-05-03 03:20
20-year member
UID 54852
Status Offline
At the top, I also really want to know what exactly this thing is used for
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
jiaxianggood +1 2007-03-16 06:00
ngd +1 2007-12-07 18:22
Floor 3 Posted 2006-05-10 09:51 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Since the person upstairs said so, I'll post the functions of general symbols. You can take a good look. Actually, they are in the forum....

Your question is related to two elements:

1. % is an ESCAPE character, usually translated as an escape character, but there are more vivid translations like escape character, evasion character, etc. That is to say, % not only escapes the specific string related to it and replaces it with a specific string, but also itself will be "escaped". And similar to the escape character "\" in the C language, double % will be escaped and escaped to single %, and four % will be escaped to double %.

2. for itself is a special command, similar to a specialized command interpreter. Because its function implementation requires executing multiple statements, it must also have the function of analyzing and processing the command line (specifically, the command line after do). And when command/cmd implements for, it will naturally borrow its own original command line analysis module. Therefore, for has the characteristic of secondary escape. The statement after do in for is analyzed and interpreted in two levels. The first level is when command/cmd reads and interprets the for command line, and the second level is when for reads and interprets the do command. It usually interprets the same command line multiple times.

Then, we can notice that when using command line parameter variables and environment variables in do, double % are not needed. That is because these variables are replaced with specific constant strings after the first level of escape, and participate in all execution processes of the for loop; while the replacement variables require dynamic changes during execution (in the sub-command line after do), and this change naturally still needs to be realized through the escape character. Therefore, the use of double % becomes a necessity.

In addition, it should also be noted that when using for in the command line, double % are not needed. This stems from the different processing methods of the command interpreter for the command line and the batch processing. In the early DOS versions, % was not regarded as an escape character in the command line, so it would not be escaped and escaped. Therefore, at that time, it was impossible to directly reference environment variables in the command line. And when using for, only one % is needed for for to perform escape and escape. In the later versions of the command interpreter, support for command line escape (mainly support for environment variables) was added, but the tradition of using single % for command line for has been retained.

And the variable delayed expansion in cmd is a special case, but it does not violate the above escape principles. It's just that the environment variables in for are no longer constants.


rmdir /S /Q %mhnet% 2>NUL 1>NUL is simply explained

The general meaning of this code is to delete the directory specified by %mhnet%. /s means to delete the subdirectories in it. /q means not to prompt for confirmation when deleting the directory tree. 1>nul means to prohibit outputting the information of correctly deleting the directory tree. 2>nul means to prohibit outputting the error information during the deletion process

Among them, 1 and 2 are both representing the address of a certain data flow input and output (NT CMD calls it a handle, MSDOS calls it a device). The following table (quoted from the "Using Command Redirection Operators (Redirection Operators" section of the WinXP help document) will list the available handles.


Handle Handle number code Description
STDIN 0 Keyboard input
STDOUT 1 Output to the command prompt window
STDERR 2 Error output to the command prompt window
UNDEFINED 3-9 These handles are individually defined by applications and various specific tools

0 Keyboard input
1 Output to the command prompt window
2 Error output to the command prompt window
3-9 These handles are individually defined by applications and various specific tools.
2 > nul means that the information of the program error is not displayed.
call attrib -r -h c:\autoexec.bat >nul
In fact, this is:
call attrib -r -h c:\autoexec.bat 1 > nul
These 1, 2, 0, etc. are all handles. To put it simply, you just need to know that 1 represents the output information, 2 represents the error information, and 0 represents the keyboard input.
If you don't understand anything, you can read more help and support.


The command line does not make excessive restrictions on the position where the redirection symbol appears. As long as the redirection symbol is immediately followed by a "character device", the following statements are equivalent:

echo Hello World> hello.txt
echo Hello> Hello.txt World
echo> Hello.txt Hello World
> hello.txt echo Hello World

In the NT series command line, the scope of the redirection changes from the entire command line to a single command statement, and is restricted by the command separators &, &&, || and statement blocks.

echo Message1> msg1.txt & echo Message2> msg2.txt
if "%target%"=="" (echo message to screen ) else (echo message to file> %target%)

In summary, >nul means to redirect the standard output request generated by this command to the null device. And because of the silent characteristic of this device, it is equivalent to shielding the output information of this statement (not hiding it); and 2>nul is to redirect and shield the standard error information output request when the program executes in error. When used together, it means shielding all possible output information of this statement.


"Redirection" is a command line feature that has existed since MSDOS. It is responsible for redirecting the input and output requests of the specified command or statement from the default "console" to other "devices" to complete. Its start mark is that the "redirection symbol" (including ">", ">>", "<" three, their respective meanings are in [1]) appears in the sentence.

Generally, the input and output requests of command line programs are completed through three internally defined "ports" (called "handles" in NT, not defined in DOS), namely standard input stdin, standard output stdout, and standard error stderr. The devices they usually point to are the console (console, code is CON). Among them, stdin points to the keyboard of the console, and stdout/stderr points to the monitor of the console. Therefore, the console usually refers to the combination of the keyboard and the monitor, which is the concept reflected on the terminal of the early mainframe. Among them, stdin can be redirected by <, stdout can be redirected by >, >>, and stderr cannot be directly redirected in DOS. Only by transferring the system control to other devices through commands such as ctty or other commands can it be indirectly completed.

"Device" refers to a device driver or port code that can control PC hardware or ports. It is usually implemented and supported by the system bottom layer or hardware driver. For example, CON, CLOCK$ implemented by IO.SYS, CONFIG$ unknown device, first serial port AUX, first parallel port PRN, all serial ports COM1~COM4, all parallel ports LPT1~LPT3, available drive letters A:-X: and the null device NUL mentioned above. There are many other devices, such as XMSXXXX0 implemented by HIMEM.SYS, EMMXXXX0 implemented by EMM386.EXE, IFS$HLP$ implemented by IFSHLP.SYS, etc.

Among these devices, there are few that can process input and output information. Only the CON, NUL, and serial or parallel port devices connected with input and output hardware (printers, MODEM, etc.). They are called "character devices", and disk files are also listed as a special character device, which greatly expands the freedom and practicability of redirection, so many people also call redirection "file redirection".

The null device NUL is a special device. Because it has no controllable PC hardware or port, but is just a fictional device or port, it only exists at the software level. Because of this, it can accept all redirected input and output requests without giving any response (in NT, it will end the input request without giving any input information, and in DOS, it will repeatedly fill 127 bytes 0 and then terminate the response). This characteristic makes it very much like a "black hole" in astronomy that can devour all matter and information, and is also very similar to the "mysterious way" in philosophy that can reverse yin and yang and generate from nothing. It exists because we need a "recycling bin" that can unconditionally absorb various redundant output information or input requests silently, just as the "black hole" is like a huge "cosmic garbage dump".



CMD is not neurotic. It's that the integer processed by set is too large. set uses two-byte storage for integers and has a 32-bit storage range limit. That is to say, its processing range is 2^-31~2^31-1. Your disk space exceeds this range and overflows.

I don't have a very good solution for this. I can only use an approximate algorithm of discarding the last three digits and then dividing by 1049.

for /f "tokens=3" %%a in ('dir /-c c:\^|find "available bytes"') do set freesize=%%a
set /a freesize=%freesize:~0,-3%/1049>nul
echo Freesize:%freesize%



> Create a file
>> Append to the back of a file
@ Prefix character. Indicates that this line is not displayed in cmd when executing. You can use echo off to turn off the display
^ Leading character for special symbols (> < &). The first one just displays aaa, the second one outputs to file bbb
echo 123456 ^> aaa
echo 1231231 > bbb
() Contains commands
(echo aa & echo bb)
, and space are the default delimiters.
; Comment, indicating that the following is a comment
: Label function
│ Pipe operation
; When the commands are the same, you can separate different targets with ;, but the execution effect remains the same. If an error occurs during the execution process, only the error report is returned, but the program will continue to execute

First of all, @ is not a command, but a special marker in DOS batch processing, only used to shield the command line echo. The following are some special markers that may be seen in the DOS command line or batch processing:
CR(0D) Command line end character
Escape(1B) ANSI escape character guide character
Space(20) Common parameter delimiter
Tab(09) ; = Uncommon parameter delimiter
+ COPY command file connection character
*? File wildcard
"" String delimiter
| Command pipe character
< > >> File redirection character
@ Command line echo shielding character
/ Parameter switch guide character
: Batch processing label guide character
% Batch processing variable guide character


Secondly, :: can indeed play the role of rem comment, and it is more concise and effective; but there are two points to note:
First, except for ::, any character line starting with : is regarded as a label in batch processing, and the content after it is directly ignored. Just to distinguish it from normal labels, it is recommended to use a special symbol that cannot be recognized by goto, that is, a special symbol other than letters and numbers immediately after :.
Second, different from rem, the character line after :: will not be echoed during execution, no matter whether the command line echo state is turned on by echo on or not, because the command interpreter does not think it is a valid command line. In this regard, rem will be more applicable than :: in some occasions; in addition, rem can be used in the config.sys file.



The following usage can also be used:
if exist command
device refers to the device loaded in the DOS system. In win98, there are usually:
AUX, PRN, CON, NUL
COM1, COM2, COM3, COM4
LPT1, LPT2, LPT3, LPT4
XMSXXXX0, EMMXXXX0
A: B: C: ...,
CLOCK$, CONFIG$, DblBuff$, IFS$HLP$
The specific content will be slightly different due to different hardware and software environments. When using these device names, the following three points need to be ensured:
1. The device really exists (except for software virtual devices)
2. The device driver has been loaded (standard devices such as aux, prn are defined by the system by default)
3. The device is ready (mainly refers to a: b: ..., com1..., lpt1...)
You can use the command mem/d | find "device" /i to check the devices loaded in your system
In addition, in the DOS system, the device is also considered a special file, and the file can also be called a character device; because the device (device) and the file are both managed by handles (handle), the handle is the name, similar to the file name. The difference is that the handle is not applied to disk management, but to memory management. The so-called device loading also means allocating a referenceable handle for it in memory.



That should be enough. For why the % symbol is used in the form of %% in batch processing, it is determined by the characteristics of batch processing and commands!!! I hope these can help you...........
Recent Ratings for This Post ( 12 in total) Click for details
RaterScoreTime
redtek +2 2006-11-08 04:43
sleet1986 +1 2007-01-13 07:17
ieutk +1 2007-03-04 13:35
studythedos +1 2007-03-24 04:22
haiou327 +2 2007-05-19 22:09
liuyun20 +1 2007-10-07 10:51
635635635 +1 2008-01-10 10:28
plp626 +4 2008-02-03 02:07
ytfy +2 2008-02-19 18:46
jonsonqf +2 2008-04-29 19:55
konkoo +2 2009-02-08 15:29
wangfangjian +4 2009-10-22 20:08
Floor 4 Posted 2006-05-10 11:01 ·  IANA 局域网IP(Private-Use)
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Top~~BagPipe's post is great
Floor 5 Posted 2006-05-10 11:18 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Brother EST, you're overpraising. I still know your strengths, heh. Knowledge is for sharing, learning together is the way to go
Floor 6 Posted 2006-07-09 18:54 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 160
Posts 75
Joined 2006-06-28 01:07
19-year member
UID 57661
Gender Male
Status Offline
Then! The characters? Can you also talk about it by the way? I often see it in the place of setlocal. For example: set /a j=!i!+1 I don't understand it very well.
Floor 7 Posted 2006-07-10 10:05 ·  中国 新疆 克拉玛依 电信
初级用户
Credits 162
Posts 44
Joined 2006-03-24 22:35
20-year member
UID 52742
Status Offline
What you said on the 3rd floor is really great!! I strongly support it
Floor 8 Posted 2006-09-30 09:52 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
The height of the 3rd floor is really high. Being able to analyze and understand so thoroughly, I must say you must be a "retired" HACKER.
Floor 9 Posted 2006-09-30 10:30 ·  中国 浙江 衢州 电信
银牌会员
★★★
Credits 1,270
Posts 548
Joined 2004-05-31 00:00
22-year member
UID 25754
Gender Male
Status Offline
That's quite a high level for bagpipe.
Floor 10 Posted 2006-09-30 11:28 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
It's a pity that Bagpipe is gone. Here is some casual posting to commemorate.

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 11 Posted 2006-09-30 13:47 ·  中国 河北 石家庄 联通
初级用户
Credits 28
Posts 12
Joined 2006-09-06 12:10
19-year member
UID 61970
Status Offline
Take it first, digest it slowly.. Thanks~~
Floor 12 Posted 2006-09-30 20:36 ·  中国 河南 新乡 联通
等待验证用户
★★★
Credits 640
Posts 314
Joined 2006-08-13 17:20
19-year member
UID 60432
Gender Male
Status Offline
Bump
Floor 13 Posted 2006-10-08 00:35 ·  中国 广东 东莞 电信
中级用户
★★
Credits 282
Posts 130
Joined 2006-09-20 22:25
19-year member
UID 63201
From 广东
Status Offline
The post about bagpipe is really good,
I'll save it first,
I'll take my time to read it later,
It seems I can't understand it all at once,
Please give me more advice if I don't understand.
Floor 14 Posted 2006-11-07 23:11 ·  中国 浙江 宁波 北仑区 电信
初级用户
Credits 42
Posts 21
Joined 2006-07-08 09:41
19-year member
UID 58221
Status Offline
Such a good tutorial definitely needs to be upvoted, although I still don't understand it.
Floor 15 Posted 2006-11-09 23:36 ·  中国 浙江 杭州 电信
新手上路
Credits 8
Posts 3
Joined 2006-09-08 10:22
19-year member
UID 62136
Status Offline
Poor foundation, not very clear, but still want to support, collect and digest slowly.
1 2 3 5 Next ›
Forum Jump: