|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『楼 主』:
批处理疑难征解:防空字符的选择
使用 LLM 解释/回答一下
To All:
经常编写批处理的人,想必对 "if string1==string2 command arguments"这样的句式耳熟能详。因为 string1/2 可以引用变量,所以有可能出现空值,所以就需要防止 string1/2 为空时 if 语句出现的语法错误。
而我们最常使用的方法就是防空字符,最初防空字符的选择有很多,比如:
if #%1==# goto end
if == goto end
if %1!==! goto end
但是,随着批处理的深入使用,以上的所有防空字符被淘汰了,关键的原因就是他们不能避免当 string1/2 中含有空格时所出现的语法错误,比如下面的语句是语法错误的:
if == goto end
此时,成对的双引号似乎成了唯一的选择:
if "%1"=="my project" goto end
然而,它仍然不是最佳的选择,如果 string1/2 中同时出现了引号和空格,那么我们所厌见的语法错误又会见面了:
if "%1"=="my project:"code for oop"" goto oop
接下来,我们该怎么办呢?别问我,我不知道答案!
Last edited by willsort on 2005-7-29 at 22:23 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-7-29 22:22 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
To All:
另一个与此相关的问题是,路径变量后的防御字符。
当我们使用环境变量引用一个路径时,一般是直接引用,比如:
%temp%\_temp.bat
但这样存在一个问题:如果%temp%是某个驱动器的根目录,比如C:\,那么以上的引用就变成了:
C:\\_temp.bat
路径中出现了双斜线,这在MS-DOS和Win9x中将会引起语法错误,所以有人采用了在变量后加一个句点的做法,即:
%temp%.\_temp.bat
这样的话,如果%temp%为根目录,引用结果就是:C:\.\_temp.bat,如果不是根目录(比如C:\temp),就是C:\temp.\_temp.bat。
这利用了句点在不同用法中所呈现出的二义性:在前一用法中,它表示当前目录,而根目录下的当前目录自然仍然是根目录;而在后一用法中,它表示目录名中主名与扩展名的分隔符,因为句点后没有实际的扩展名,所以C:\temp.仍然表示的是C:\temp目录。
但是,这带来了一个新问题:如果路径中出现了相对路径的特殊引用符,该怎么办?比如,如果%temp%是表示当前目录的 . 或者上一级目录的 ..,那么 %temp%.\_temp.bat 的引用就变成了:
..\_temp.bat 或者 ...\_temp.bat
这显然又不是我们所需要的结果,那么我们该怎么办呢?
答案在你们的手里 :-)
Last edited by willsort on 2005-8-1 at 11:34 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-8-1 11:28 |
|
|
rubik
初级用户
 
积分 102
发帖 35
注册 2006-3-16
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2005-7-29 22:22:
To All:
经常编写批处理的人,想必对 "if string1==string2 command arguments"这样的句式耳熟能详。因为 string1/2 可以引用变量,所以有可能出现空值,所以就需要防止 string1/2 为空时 if 语句出现的语法错误。
而我们最常使用的方法就是防空字符,最初防空字符的选择有很多,比如:
if #%1==# goto end
if == goto end
if %1!==! goto end
但是,随着批处理的深入使用,以上的所有防空字符被淘汰了,关键的原因就是他们不能避免当 string1/2 中含有空格时所出现的语法错误,比如下面的语句是语法错误的:
if == goto end
此时,成对的双引号似乎成了唯一的选择:
if "%1"=="my project" goto end
然而,它仍然不是最佳的选择,如果 string1/2 中同时出现了引号和空格,那么我们所厌见的语法错误又会见面了:
if "%1"=="my project:"code for oop"" goto oop
接下来,我们该怎么办呢?别问我,我不知道答案!
还好 2K 中的CMD没有此等问题
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
|
|
2007-2-16 15:43 |
|
|
gne3
高级用户
    DOS学徒
积分 526
发帖 252
注册 2007-2-12
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
willsort就是强
研究的很深了
willsort is really powerful
Has studied very deeply
|
|
2007-2-16 23:06 |
|
|
0451lym
高级用户
   
积分 760
发帖 357
注册 2005-10-10
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
这是我前几天碰到的问题,没办法只好如此解决,不知道有没有更好的办法了?
: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 ]
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 ]
|
|
2007-2-17 00:55 |
|
|
llztt
中级用户
  
积分 204
发帖 44
注册 2003-8-8
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
关于第一个问题,看过XUSEN批处理后可以找到几种处理的办法,一个较好的办法是用批处理的参数办法,因为批处理会把传递来的参数以空格为分界符来分之,用来去掉变量中的空字符倒是个好办法,不过应对判断变量与值是否相等得需要变通一下了
第二个问题有些过于牵强批了,如果非要应用之,难道还得用批处理先检验该变量??或者等谁来做个小程序集成该功能吧
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.
|
|
2007-2-18 23:08 |
|
|
koala
初级用户
  Batchs上議院參議長
积分 199
发帖 105
注册 2007-6-5 来自 江苏
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
willsort 如果不离开本群的话相信这帖子就不会这么冷清了
willsort If you don't leave this group, I believe this post won't be so deserted
|

『生如夏花之绚烂
死若秋叶之静美』 dos做到了 |
|
2007-8-23 15:44 |
|
|
zerocq
中级用户
  
积分 458
发帖 196
注册 2006-10-5
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
第一个问题:
if ""%1""==""my project:"code for oop""" goto oop
=号两边再各添一对""行吗
First question:
if """"%1""""==""my project:"code for oop"""" goto oop
Can we add another pair of "" on both sides of the = sign?
|
|
2007-10-1 00:29 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by zerocq at 2007-10-1 00:29:
第一个问题:
if ""%1""==""my project:"code for oop""" goto oop
=号两边再各添一对""行吗
测试了一下,也不行貌似。
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.
|
|
2007-10-1 14:08 |
|
|
crazycurl
新手上路

积分 3
发帖 2
注册 2009-12-7
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
探讨问题一:
其实我个人做法是这样的,把要防止空的变量在For循环中先做一些处理不就行了?
在用变量的句子里利用For循环一下变量,原理是For循环不回执行空行。所以如果值是空的话,会直接跳出,不回执行需要用到变量的句子。
但问题又来了,就是那样会嵌套很多层,对于阅读和维护不方便
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
|
|
2009-12-30 15:07 |
|