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-01 10:08
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Troubleshooting in Batch Processing: Selection of Anti-Air Characters View 2,653 Replies 9
Original Poster Posted 2005-07-29 22:22 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2005-08-01 11:28 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2007-02-16 15:43 ·  中国 重庆 沙坪坝区 联通
初级用户
Credits 102
Posts 35
Joined 2006-03-16 11:56
20-year member
UID 52168
Status Offline
Originally posted by willsort at 2005-7-29 22:22:
To All:

People who often write batch scripts must be familiar with the sentence pattern "if string1==string2 command arguments". Because string1/2 can reference variables, there may be empty values, so it is necessary to prevent syntax errors in the if statement when string1/2 is empty.

And the most commonly used method is to prevent empty characters. Originally, 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 empty character prevention methods 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 error we dislike will appear again:

if "%1"=="my project:"code for oop"" goto oop

Next, what should we do? Don't ask me, I don't know the answer!



Fortunately, the CMD in 2K doesn't have such problems
Floor 4 Posted 2007-02-16 23:06 ·  中国 浙江 台州 温岭市 电信
高级用户
★★
DOS学徒
Credits 526
Posts 252
Joined 2007-02-12 05:35
19-year member
UID 79286
Gender Male
Status Offline
willsort is really powerful
Has studied very deeply
Floor 5 Posted 2007-02-17 00:55 ·  中国 黑龙江 哈尔滨 电信
高级用户
★★★
Credits 760
Posts 357
Joined 2005-10-10 22:33
20-year member
UID 43332
Status Offline
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 ]
Floor 6 Posted 2007-02-18 23:08 ·  中国 山东 滨州 博兴县 联通
中级用户
Credits 204
Posts 44
Joined 2003-08-08 00:00
22-year member
UID 8224
Gender Male
Status Offline
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.
Floor 7 Posted 2007-08-23 15:44 ·  中国 江苏 宿迁 电信
初级用户
★★
Batchs上議院參議長
Credits 199
Posts 105
Joined 2007-06-05 12:00
19-year member
UID 90300
Gender Male
From 江苏
Status Offline
willsort If you don't leave this group, I believe this post won't be so deserted
『生如夏花之绚烂
死若秋叶之静美』 dos做到了
Floor 8 Posted 2007-10-01 00:29 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
First question:
if """"%1""""==""my project:"code for oop"""" goto oop
Can we add another pair of "" on both sides of the = sign?
Floor 9 Posted 2007-10-01 14:08 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by zerocq at 2007-10-1 00:29:
First question:
if ""%1""==""my project:"code for oop""" goto oop
Can we add another pair of "" on both sides of the equal sign?

Tested it, it still doesn't seem to work.
Floor 10 Posted 2009-12-30 15:07 ·  中国 广东 江门 电信
新手上路
Credits 3
Posts 2
Joined 2009-12-07 18:22
16-year member
UID 156087
Gender Male
Status Offline
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
Forum Jump: