![]() |
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-15 04:38 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » Troubleshooting in Batch Processing: Selection of Anti-Air Characters |
| Printable Version 2,865 / 9 |
| Floor1 willsort | Posted 2005-07-29 22:22 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
To All:
People who often write batch scripts must be very familiar with the sentence pattern "if string1==string2 command arguments". Because string1/2 can refer to variables, there may be empty values, so it is necessary to prevent syntax errors when if statements occur when string1/2 is empty. The most commonly used method is to prevent empty characters. Initially, there were many choices for preventing empty characters, such as: if #%1==# goto end if == goto end if %1!==! goto end However, with the in-depth use of batch scripts, all the above methods of preventing empty characters have been eliminated. The key reason is that they cannot avoid syntax errors when string1/2 contains spaces. For example, the following statement is a syntax error: if == goto end At this time, the paired double quotes seem to be the only choice: if "%1"=="my project" goto end However, it is still not the best choice. If both quotes and spaces appear in string1/2, the syntax errors we dislike will appear again: if "%1"=="my project:"code for oop"" goto oop What should we do next? Don't ask me, I don't know the answer! [ Last edited by willsort on 2005-7-29 at 22:23 ] |
|
| Floor2 willsort | Posted 2005-08-01 11:28 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
To All:
Another related issue is the defense character after the path variable. When we use an environment variable to reference a path, we generally reference it directly, for example: %temp%\_temp.bat But there is a problem here: if %temp% is the root directory of a certain drive, such as C:\, then the above reference becomes: C:\\_temp.bat There are double slashes in the path, which will cause a syntax error in MS-DOS and Win9x, so some people use the method of adding a dot after the variable, that is: %temp%.\_temp.bat In this case, if %temp% is the root directory, the reference result is: C:\.\_temp.bat, and if it is not the root directory (such as C:\temp), it is C:\temp.\_temp.bat. This uses the ambiguity of the dot in different usages: in the previous usage, it represents the current directory, and the current directory under the root directory is naturally still the root directory; in the latter usage, it represents the directory name The separator between the main name and the extension, because there is no actual extension after the dot, so C:\temp. still represents the C:\temp directory. However, this brings a new problem: what if there are special reference characters for relative paths in the path? For example, if %temp% represents the current directory . or the previous level directory .., then the reference of %temp%.\_temp.bat becomes: ..\_temp.bat or ...\_temp.bat This is obviously not the result we need, so what should we do? The answer is in your hands :-) [ Last edited by willsort on 2005-8-1 at 11:34 ] |
|
| Floor3 rubik | Posted 2007-02-16 15:43 |
| 初级用户 Posts 35 Credits 102 | |
Originally posted by willsort at 2005-7-29 22:22: Fortunately, the CMD in 2K doesn't have such problems |
|
| Floor4 gne3 | Posted 2007-02-16 23:06 |
| 高级用户 Posts 252 Credits 526 | |
|
willsort is really powerful
Has studied very deeply |
|
| Floor5 0451lym | Posted 2007-02-17 00:55 |
| 高级用户 Posts 357 Credits 760 | |
|
This is a problem I encountered a few days ago, and I had no choice but to solve it like this. I don't know if there is a better way.
:01 ECHO %LA%>%temp%\PD.TXT TYPE %temp%\PD.TXT|find /i "ECHO is off">NUL if ERRORLEVEL 1 goto 02 DEL %temp%\PD.TXT MYSET LA=AAAAA>NUL :02 ....... [ Last edited by 0451lym on 2007-2-17 at 01:29 AM ] |
|
| Floor6 llztt | Posted 2007-02-18 23:08 |
| 中级用户 Posts 44 Credits 204 | |
|
Regarding the first question, after looking at XUSEN's batch processing, several ways to handle it can be found. A better way is to use the parameter method of batch processing, because the batch processing will divide the passed parameters by spaces as delimiters, which is a good way to remove empty characters in variables, but some flexibility is needed to deal with judging whether the variable is equal to the value.
The second question is a bit far-fetched. If it must be applied, does it still need to use batch processing to check the variable first?? Or wait for someone to make a small program to integrate this function. |
|
| Floor7 koala | Posted 2007-08-23 15:44 |
| 初级用户 Posts 105 Credits 199 From 江苏 | |
|
willsort If you don't leave this group, I believe this post won't be so deserted
|
|
| Floor8 zerocq | Posted 2007-10-01 00:29 |
| 中级用户 Posts 196 Credits 458 | |
|
First question:
if """"%1""""==""my project:"code for oop"""" goto oop Can we add another pair of "" on both sides of the = sign? |
|
| Floor9 lxmxn | Posted 2007-10-01 14:08 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by zerocq at 2007-10-1 00:29: Tested it, it still doesn't seem to work. |
|
| Floor10 crazycurl | Posted 2009-12-30 15:07 |
| 新手上路 Posts 2 Credits 3 | |
|
Discussion question one:
Actually, my personal approach is like this. Can't I just do some processing on the variables that need to be prevented from being empty in the For loop? In the sentences where variables are used, use the For loop on the variables. The principle is that the For loop will not execute empty lines. So if the value is empty, it will jump out directly and will not execute the sentences that need to use the variables. But the problem comes again, that is, there will be many nested layers, which is not convenient for reading and maintenance |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |