Windows XP中的批处理命令已经同DOS下的大不相同,我发现IF命令也强大了很多。
在批处理程序中执行条件处理。如果 if 命令中指定的条件为真,Windows 将执行该条件后的命令。如果条件为假,Windows 将忽略 if 子句中的命令,并执行 else 子句中的任何命令(如果已经指定了命令)。这和以前DOS下的相同。
if [not] errorlevel number command [else expression]
if [not] string1==string2 command [else expression]
if [not] exist filename command [else expression]
if [not] string1==string2 command [else expression]
if [not] exist filename command [else expression]
Windows XP增强了一些功能,如可启用了命令扩展:
if [/i] string1 compare-op string2 command [else expression]
if cmdextversion number command [else expression]
if defined variable command [else expression]
if cmdextversion number command [else expression]
if defined variable command [else expression]
compare-op,为以下三字母比较操作符中的一个:
EQU 等于(equal)
NEQ 不等于(not equal)
LSS 少于(less)
LEQ 少于或等于(less or equal)
GTR 大于(greater)
GEQ 大于或等于(greater or equal)
/i
指定 /i 开关时,将强制字符串比较按忽略大小写情况进行。/i 开关也可以用于 if 的 string1==string2 格式。这些比较是通用的,如果 string1 和 string2 都由所有的数字组成,那么字符串将被转换为数字并且将执行数字比较。
if...else... 要符合下面的写法。这个写法很接近常规的C/PHP语言,而且非常有用。
if exist filename (
cmdline1
cmdline2
cmdline3
) else (
cmdline4
cmdline5
)
cmdline1
cmdline2
cmdline3
) else (
cmdline4
cmdline5
)
一个范例:
@echo off>nul
setlocal
set KDS_ID = 242
set KDS_NAME = " - Saito Asami"
if %KDS_ID% lss 237 (
copy kds1.png %TEMP%\kds.cmd
) else (
copy kds2.png %TEMP%\kds.cmd
)
delete kds1.png
delete kds2.png
start %TEMP%\kds.cmd %KDS_ID% %KDS_NAME%
endlocal
setlocal
set KDS_ID = 242
set KDS_NAME = " - Saito Asami"
if %KDS_ID% lss 237 (
copy kds1.png %TEMP%\kds.cmd
) else (
copy kds2.png %TEMP%\kds.cmd
)
delete kds1.png
delete kds2.png
start %TEMP%\kds.cmd %KDS_ID% %KDS_NAME%
endlocal
