DOSforever 
金牌会员
 
      
 
  
  
积分 4639 
发帖 2239 
注册 2005-1-30 
状态 离线
 | 
『第 2 楼』:
 
 
使用 LLM 解释/回答一下
  
4DOS 的 if 可以判断数值大小 
 
 
4DOS Help Topic:  IF 
 
 
Purpose:  Execute a command if a condition or set of conditions is true.  
 
Format:   IF  condition   
          condition ...] command  
              or  
          IF  condition   
          condition ...] (command) ELSE (command)  
 
          condition:  A test to determine if the command should be executed. 
          command:    The command to execute if the condition is true.  
 
See also: IFF, @IF.  
 
Usage  
 
IF is normally used only in aliases and batch files.  It is always followed  
by one or more conditions and then a command.  First, the conditions are  
evaluated.  If they are true, the command is executed.  Otherwise, the  
command is ignored.  If you add a NOT before a condition, the command is  
executed only when the condition is false.  
 
You can link conditions with .AND., .OR., or .XOR., and you can group  
conditions with parentheses (see Combining Tests below).  You can also nest  
IF statements.  
 
The conditions can test strings, numbers, the existence of a file or  
subdirectory, the exit code returned by the preceding external command, and  
the existence of aliases and internal commands.  
 
The command can be an alias, an internal command, an external command, or a  
batch file.  The entire IF statement, including all conditions and the  
command, must fit on one line.  
 
Some examples of IF conditions and commands are included below; additional  
examples are included in the EXAMPLES.BTM file which came with 4DOS.  
 
You can use command grouping to execute multiple commands if the condition  
is true.  For example, the following command tests if any .TXT files exist.  
If they do, they are copied to drive A: and their extensions are changed to  
.TXO:  
 
     if exist *.txt (copy *.txt a: ^ ren *.txt *.txo)  
 
(Note that the IFF command provides a more structured method of executing  
multiple commands if a condition or set of conditions is true.)  
 
If you receive a stack overflow error when using IF in complex, nested  
command sequences, see the notes under the StackSize directive.  
 
Conditions  
 
The conditional tests listed in the following sections are available in both 
the IF and IFF commands.  They fit into two categories:  string and numeric  
tests, and status tests.  The tests can use environment variables, internal  
variables and variable functions, file names, literal text, and numeric  
values as their arguments.  
 
String and Numeric Tests  
 
Six test conditions can be used to test character strings.  The same  
conditions are available for both numeric and normal text strings (see below 
for details).  In each case you enter the test as:  
 
     string1 operator string2  
 
The operator defines the type of test (equal, greater than or equal, and so  
on).  You should always use spaces on both sides of the operator.  The  
operators are:  
 
     Operator     Tests  
 
     EQ or ==     string1 equal to string2  
     NE or !=     string1 not equal to string2  
     LT           string1 less than string2  
     LE           string1 less than or equal to string2  
     GE           string1 greater than or equal to string2  
     GT           string1 greater than string2  
 
When IF compares two character strings, it will use either a numeric  
comparison or a string comparison.  A numeric comparison treats the strings  
as numeric values and tests them arithmetically.  A string comparison treats 
the strings as text.  
 
The difference between numeric and string comparisons is best explained by  
looking at the way two values are tested.  For example, consider comparing  
the values 2 and 19.  Numerically, 2 is smaller, but as a string it is  
"larger" because its first digit is larger than the first digit of 19.  So  
the first of these conditions will be true, and the second will be false:  
 
     if 2 lt 19 ...  
     if "2" lt "19" ...  
 
IF determines which kind of test to do by examining the first character of  
each string.  If both strings begin with a numeric character (a digit, sign, 
or decimal separator), a numeric comparison is used.  (If a string begins  
with a decimal separator it is not considered numeric unless the next  
character is a digit, and there are no more decimal separators within the  
string.  For example, ".07" is numeric, but ".a" and ".07.50" are not.)  If  
either value is non-numeric, a string comparison is used.  To force a string 
comparison when both values are or may be numeric, use double quotes around  
the values you are testing, as shown above.  Because the double quote is not 
a numeric character, IF performs a string comparison.  
 
Case differences are ignored in string comparisons.  If two strings begin  
with the same text but one is shorter, the shorter string is considered to  
be "less than" the longer one.  For example, "a" is less than "abc", and  
"hello_there" is greater than "hello".  
 
When you compare text strings, you may need to enclose the arguments in  
double quotes in order to avoid syntax errors which can occur if one of the  
argument values is empty (e.g., due to an environment variable which has  
never been assigned a value).  This technique will not work for numeric  
comparisons, as the quotes will force a string compare, so with numeric  
tests you must be sure that all variables are assigned values before the  
test is done.  
 
Numeric comparisons work with both integer and decimal values.  The values  
to be compared must contain only numeric digits, decimal points, and an  
optional sign (+ or -).  The number may contain up to 20 digits to the left  
of the decimal point, and 10 digits to the right.  
 
Internal variables and variable functions are very powerful when combined  
with string and numeric comparisons.  They allow you to test the state of  
your system, the characteristics of a file, date and time information, or  
the result of a calculation.  You may want to review the variables and  
variable functions when determining the best way to set up an IF test.  
 
This batch file fragment tests for a string value:  
 
     input "Enter your selection : " %%cmd  
     if "%cmd" == "WP" goto wordproc  
     if "%cmd" NE "GRAPHICS" goto badentry  
 
This example calls GO.BTM if the first two characters in the file MYFILE are 
"GO":  
 
     if "%@left]" == "GO" call go.btm  
 
The next two examples test whether there is more than 500 KBytes of free  
memory or more than 2 MBytes of free EMS memory (the EMS example only  
applies to 4DOS):  
 
     c:\> if %@dosmem gt 500 echo Over 500K free  
     c:\> if %@ems gt 2 echo Over 2 MB EMS free  
 
Status Tests  
 
These conditions test the system or 4DOS status.  You can use internal  
variables and variable functions to test many other parts of the system  
status.  
 
     DEFINED variable  
          If the variable exists in the environment, the condition is  
          true.  This is equivalent to testing whether the variable is not  
          empty, for example the following two commands are equivalent:  
 
               if defined abc echo Hello  
               if "%abc" != "" echo Hello  
 
     ERRORLEVEL  n  
          This test retrieves the exit code of the preceding external  
          program.  By convention, programs return an exit code of 0 when  
          they are successful and a number between 1 and 255 to indicate an  
          error.  The condition can be any of the operators listed above  
          (EQ, !=, GT, etc.).  If no operator is specified, the  
          default is GE.  The comparison is done numerically.  
 
          Not all programs return an explicit exit code.  For programs  
          which do not, the behavior of ERRORLEVEL is undefined.  
 
     EXIST filename  
          If the file exists, the condition is true.  You can use wildcards  
          in the filename, in which case the condition is true if any file  
          matching the wildcard name exists.  
 
          Do not use IF EXIST to test for existence of a directory (use IF  
          ISDIR instead).  Due to variations in operating system internals,  
          IF EXIST will not return consistent results when used to test for  
          the existence of a directory.  
 
     ISALIAS aliasname  
          If the name is defined as an alias, the condition is true.  
 
     ISDIR | DIREXIST path  
          If the subdirectory exists, the condition is true.  For  
          compatibility with the DR-DOS family, DIREXIST may be used as  
          a synonym for ISDIR.  
 
     ISFUNCTION functionname  
          If the name is defined as a user-defined function, the condition  
          is true.  
 
     ISINTERNAL command  
          If the specified command is an active internal command, the  
          condition is true.  Commands can be activated and deactivated  
          with the SETDOS /I command.  
 
     ISLABEL labelname  
          If the specified name exists as a label in the current batch  
          file, the condition is true.  Labels may be one or more words  
          long.  
 
The first batch file fragment below tests for the existence of A:\JAN.DOC  
before copying it to drive C (this avoids an error message if the file does  
not exist):  
 
     if exist a:\jan.doc copy a:\jan.doc c:\  
 
This example tests the exit code of the previous program and stops all batch 
file processing if an error occurred:  
 
     if errorlevel == 0 goto success  
     echo "External Error -- Batch File Ends!"  
     cancel  
 
Combining Tests  
 
You can negate the result of any test with NOT, and combine tests of any  
type with .AND., .OR., and .XOR..  
 
When two tests are combined with .AND., the result is true if both  
individual tests are true.  When two tests are combined with .OR., the  
result is true if either (or both) individual tests are true.  When two  
tests are combined with .XOR., the result is true only if one of the tests  
is true and the other is false.  
 
This example runs a program called DATALOAD if today is Monday or Tuesday:  
 
        if "%_dow" == "Mon" .or. "%_dow" == "Tue" dataload  
 
Test conditions are always scanned from left to right -- there is no implied 
order of precedence, as there is in some programming languages.  You can,  
however, force a specific order of testing by grouping conditions with  
parentheses, for example (enter this on one line):  
 
     if (%a == 1 .or. (%b == 2 .and. %c == 3)) echo something  
 
Parentheses can only be used when the portion of the condition inside the  
parentheses contains at least one ".and.", ".or.", or ".xor.".  Parentheses  
on a simple condition which does not combine two or more tests will be taken 
as part of the string to be tested, and will probably make the test fail.   
For example, the first of these IF tests would fail; the second would  
succeed:  
 
     if (a == a) ...  
     if (a == a .and. b == b) ...  
 
Parentheses can be nested.  Under 4DOS, the permissible nesting level  
depends on the amount of free space in 4DOS's internal stack; if you receive 
a stack overflow error when using nested parentheses, see the notes under  
the StackSize directive.  
  
4DOS 的 if 可以判断数值大小 
 
 
4DOS 帮助主题:IF 
 
目的:如果条件或一组条件为真,则执行命令。 
 
格式:IF  条件  条件 ...] 命令  
          或者  
          IF  条件  条件 ...] (命令) ELSE (命令)  
 
          条件:用于确定是否应执行命令的测试。 
          命令:条件为真时要执行的命令。  
 
另见:IFF、@IF。  
 
用法  
 
IF 通常仅在别名和批处理文件中使用。它始终后跟一个或多个条件,然后是一个命令。首先,评估条件。如果为真,则执行命令。否则,忽略该命令。如果在条件前添加 NOT,则仅当条件为假时才执行命令。  
 
您可以使用 .AND.、.OR. 或 .XOR. 链接条件,并且可以使用括号对条件进行分组(参见组合测试部分)。您还可以嵌套 IF 语句。  
 
条件可以测试字符串、数字、文件或子目录的存在、前一个外部命令返回的退出代码,以及别名和内部命令的存在。  
 
命令可以是别名、内部命令、外部命令或批处理文件。整个 IF 语句,包括所有条件和命令,必须放在一行上。  
 
以下包含了一些 IF 条件和命令的示例;4DOS 附带的 EXAMPLES.BTM 文件中包含了更多示例。  
 
您可以使用命令分组来在条件为真时执行多个命令。例如,以下命令测试是否存在任何 .TXT 文件。如果存在,则将它们复制到 A 驱动器并将其扩展名更改为 .TXO:  
 
     if exist *.txt (copy *.txt a: ^ ren *.txt *.txo)  
 
(请注意,IFF 命令提供了一种更结构化的方法,用于在条件或一组条件为真时执行多个命令。)  
 
如果在复杂的嵌套命令序列中使用 IF 时收到堆栈溢出错误,请参阅 StackSize 指令下的注释。  
 
条件  
 
以下部分列出的条件测试在 IF 和 IFF 命令中都可用。它们分为两类:字符串和数字测试,以及状态测试。测试可以使用环境变量、内部变量和变量函数、文件名、字面文本和数值作为其参数。  
 
字符串和数字测试  
 
六个测试条件可用于测试字符串。相同的条件适用于数字和普通文本字符串(有关详细信息,请参见下文)。在每种情况下,您输入测试的方式为:  
 
     字符串1 运算符 字符串2  
 
运算符定义了测试的类型(等于、大于或等于等)。您应该始终在运算符的两侧使用空格。运算符如下:  
 
     运算符     测试  
 
     EQ 或 ==     字符串1 等于 字符串2  
     NE 或 !=     字符串1 不等于 字符串2  
     LT           字符串1 小于 字符串2  
     LE           字符串1 小于或等于 字符串2  
     GE           字符串1 大于或等于 字符串2  
     GT           字符串1 大于 字符串2  
 
当 IF 比较两个字符串时,它将使用数值比较或字符串比较。数值比较将字符串视为数值并进行算术测试。字符串比较将字符串视为文本。  
 
数值比较和字符串比较之间的区别最好通过查看两个值的测试方式来解释。例如,考虑比较值 2 和 19。从数值上看,2 更小,但作为字符串,它“更大”,因为它的第一个数字大于 19 的第一个数字。所以以下第一个条件为真,第二个为假:  
 
     if 2 lt 19 ...  
     if "2" lt "19" ...  
 
IF 通过检查每个字符串的第一个字符来确定要进行哪种测试。如果两个字符串都以数字字符(数字、符号或小数点分隔符)开头,则使用数值比较。(如果字符串以小数点分隔符开头,除非下一个字符是数字,并且字符串中没有更多的小数点分隔符,否则它不被视为数字。例如,“.07”是数字,但“.a”和“.07.50”不是。)如果任何一个值是非数字的,则使用字符串比较。要在两个值都是或可能是数字时强制进行字符串比较,请像上面所示的那样在您要测试的值周围加上双引号。因为双引号不是数字字符,所以 IF 执行字符串比较。  
 
 
在字符串比较中,大小写差异被忽略。如果两个字符串以相同的文本开头但其中一个较短,则较短的字符串被认为比长的字符串“小”。例如,“a”小于“abc”,“hello_there”大于“hello”。  
 
当您比较文本字符串时,您可能需要将参数用双引号括起来,以避免由于其中一个参数值为空(例如,由于从未被赋值的环境变量)而可能发生的语法错误。这种技术不适用于数值比较,因为引号会强制进行字符串比较,所以对于数值测试,您必须确保在测试之前所有变量都已赋值。  
 
数值比较适用于整数和小数值。要比较的值必须仅包含数字、小数点和可选的符号(+ 或 -)。数字的小数点左侧最多可以有 20 位数字,右侧最多可以有 10 位数字。  
 
内部变量和变量函数与字符串和数字比较结合使用时非常强大。它们允许您测试系统的状态、文件的特性、日期和时间信息或计算结果。在确定设置 IF 测试的最佳方法时,您可能需要查看变量和变量函数。  
 
以下批处理文件片段测试字符串值:  
 
     input "Enter your selection : " %%cmd  
     if "%cmd" == "WP" goto wordproc  
     if "%cmd" NE "GRAPHICS" goto badentry  
 
此示例在文件 MYFILE 的前两个字符是“GO”时调用 GO.BTM:  
 
     if "%@left]" == "GO" call go.btm  
 
接下来的两个示例测试是否有超过 500 KBytes 的可用内存或超过 2 MBytes 的可用 EMS 内存(EMS 示例仅适用于 4DOS):  
 
     c:\> if %@dosmem gt 500 echo Over 500K free  
     c:\> if %@ems gt 2 echo Over 2 MB EMS free  
 
状态测试  
 
这些条件测试系统或 4DOS 的状态。您可以使用内部变量和变量函数来测试系统状态的许多其他部分。  
 
     DEFINED variable  
          如果变量存在于环境中,则条件为真。这等效于测试变量是否不为空,例如以下两个命令等效:  
 
               if defined abc echo Hello  
               if "%abc" != "" echo Hello  
 
     ERRORLEVEL  n  
          此测试检索前一个外部程序的退出代码。按照惯例,程序成功时返回退出代码 0,返回 1 到 255 之间的数字表示错误。条件可以是上述任何运算符(EQ、!=、GT 等)。如果未指定运算符,则默认是 GE。比较是按数值进行的。  
 
          并非所有程序都返回显式的退出代码。对于不返回的程序,ERRORLEVEL 的行为未定义。  
 
     EXIST filename  
          如果文件存在,则条件为真。您可以在文件名中使用通配符,在这种情况下,如果任何与通配符名称匹配的文件存在,则条件为真。  
 
          不要使用 IF EXIST 来测试目录的存在(使用 IF ISDIR 代替)。由于操作系统内部的差异,当使用 IF EXIST 来测试目录的存在时,不会返回一致的结果。  
 
     ISALIAS aliasname  
          如果名称定义为别名,则条件为真。  
 
     ISDIR | DIREXIST path  
          如果子目录存在,则条件为真。为了与 DR-DOS 系列兼容,DIREXIST 可用作 ISDIR 的同义词。  
 
     ISFUNCTION functionname  
          如果名称定义为用户定义的函数,则条件为真。  
 
     ISINTERNAL command  
          如果指定的命令是活动的内部命令,则条件为真。可以使用 SETDOS /I 命令激活和停用命令。  
 
     ISLABEL labelname  
          如果指定的名称作为标签存在于当前批处理文件中,则条件为真。标签可以是一个或多个单词长。  
 
以下第一个批处理文件片段在将 A:\JAN.DOC 复制到 C 驱动器之前测试其是否存在(这避免了如果文件不存在时的错误消息):  
 
     if exist a:\jan.doc copy a:\jan.doc c:\  
 
此示例测试前一个程序的退出代码,如果发生错误则停止所有批处理文件处理:  
 
     if errorlevel == 0 goto success  
     echo "External Error -- Batch File Ends!"  
     cancel  
 
组合测试  
 
您可以使用 NOT 否定任何测试的结果,并使用 .AND.、.OR. 和 .XOR. 组合任何类型的测试。  
 
当两个测试用 .AND. 组合时,仅当两个单独的测试都为真时结果才为真。当两个测试用 .OR. 组合时,仅当两个单独的测试中的一个(或两个)为真时结果才为真。当两个测试用 .XOR. 组合时,仅当其中一个测试为真且另一个为假时结果才为真。  
 
此示例在今天是星期一或星期二时运行名为 DATALOAD 的程序:  
 
        if "%_dow" == "Mon" .or. "%_dow" == "Tue" dataload  
 
测试条件始终从左到右扫描——没有隐含的优先级顺序,就像在某些编程语言中那样。但是,您可以通过使用括号对条件进行分组来强制特定的测试顺序,例如(在一行上输入此内容):  
 
     if (%a == 1 .or. (%b == 2 .and. %c == 3)) echo something  
 
括号只能在条件中括号内的部分包含至少一个“.and.”、“.or.”或“.xor.”时使用。在不组合两个或多个测试的简单条件上的括号将被视为要测试的字符串的一部分,并且可能会使测试失败。例如,以下第一个 IF 测试将失败;第二个将成功:  
 
     if (a == a) ...  
     if (a == a .and. b == b) ...  
 
括号可以嵌套。在 4DOS 下,允许的嵌套级别取决于 4DOS 内部堆栈中的可用空间;如果使用嵌套括号时收到堆栈溢出错误,请参阅 StackSize 指令下的注释。  
  
    
 
  
  |  
                  
  
                    DOS倒下了,但永远不死 
DOS NEVER DIES ! 
 
投票调查: 
http://www.cn-dos.net/forum/viewthread.php?tid=46187 
 
本人尚未解决的疑难问题: 
http://www.cn-dos.net/forum/viewthread.php?tid=15135 
http://www.cn-dos.net/forum/viewthread.php?tid=47663 
http://www.cn-dos.net/forum/viewthread.php?tid=48747 |   
 |