![]() |
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-08-06 04:05 |
48,039 topics / 350,124 posts / today 2 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » Who can explain the "%"? |
| Printable Version 14,550 / 72 |
| Floor1 waynebeat | Posted 2006-05-09 22:55 |
| 初级用户 Posts 28 Credits 84 | |
|
“%” This thing, one or two are understandable
But when there are too many, it's dizzying Which expert can explain it? |
|
| Floor2 kingljp | Posted 2006-05-10 01:47 |
| 初级用户 Posts 29 Credits 80 | |
|
At the top, I also really want to know what exactly this thing is used for
|
|
| Floor3 bagpipe | Posted 2006-05-10 09:51 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
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........... |
|
| Floor4 electronixtar | Posted 2006-05-10 11:01 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
Top~~BagPipe's post is great
|
|
| Floor5 bagpipe | Posted 2006-05-10 11:18 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
Brother EST, you're overpraising. I still know your strengths, heh. Knowledge is for sharing, learning together is the way to go
|
|
| Floor6 buddiyar | Posted 2006-07-09 18:54 |
| 初级用户 Posts 75 Credits 160 | |
|
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.
|
|
| Floor7 biobio | Posted 2006-07-10 10:05 |
| 初级用户 Posts 44 Credits 162 | |
|
What you said on the 3rd floor is really great!! I strongly support it
|
|
| Floor8 lxmxn | Posted 2006-09-30 09:52 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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.
|
|
| Floor9 weilong888 | Posted 2006-09-30 10:30 |
| 银牌会员 Posts 548 Credits 1,270 | |
|
That's quite a high level for bagpipe.
|
|
| Floor10 electronixtar | Posted 2006-09-30 11:28 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
It's a pity that Bagpipe is gone. Here is some casual posting to commemorate.
|
|
| Floor11 wlnwcn | Posted 2006-09-30 13:47 |
| 初级用户 Posts 12 Credits 28 | |
|
Take it first, digest it slowly.. Thanks~~
|
|
| Floor12 tangtai | Posted 2006-09-30 20:36 |
| 等待验证用户 Posts 314 Credits 640 | |
|
Bump
|
|
| Floor13 jieok3375 | Posted 2006-10-08 00:35 |
| 中级用户 Posts 130 Credits 282 From 广东 | |
|
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. |
|
| Floor14 d1998o | Posted 2006-11-07 23:11 |
| 初级用户 Posts 21 Credits 42 | |
|
Such a good tutorial definitely needs to be upvoted, although I still don't understand it.
|
|
| Floor15 music0 | Posted 2006-11-09 23:36 |
| 新手上路 Posts 3 Credits 8 | |
|
Poor foundation, not very clear, but still want to support, collect and digest slowly.
|
|
| 1 2 3 4 … 5 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |