我该如何压缩这个?

时间:2015-03-27 15:05:59

标签: batch-file if-statement for-loop exists

实际上比这要长得多。有什么更好的方法呢?我正在尝试编写一个查看文件的决策程序,并确定要转到哪个脚本。

IF EXIST *x71c48* (GOTO script-a) else goto script-b
IF EXIST *x72c48* (GOTO script-a) else goto script-b
IF EXIST *x73c48* (GOTO script-a) else goto script-b
IF EXIST *x74c48* (GOTO script-a) else goto script-b
IF EXIST *x75c48* (GOTO script-a) else goto script-b
IF EXIST *x76c48* (GOTO script-a) else goto script-b
IF EXIST *x77c48* (GOTO script-a) else goto script-b
IF EXIST *x78c48* (GOTO script-a) else goto script-b
IF EXIST *x79c48* (GOTO script-a) else goto script-b
IF EXIST *x80c48* (GOTO script-a) else goto script-b
IF EXIST *x81c48* (GOTO script-a) else goto script-b
IF EXIST *x82c48* (GOTO script-a) else goto script-b
IF EXIST *x83c48* (GOTO script-a) else goto script-b
IF EXIST *x84c48* (GOTO script-a) else goto script-b
IF EXIST *x85c48* (GOTO script-a) else goto script-b
IF EXIST *x86c48* (GOTO script-a) else goto script-b
IF EXIST *x87c48* (GOTO script-a) else goto script-b
IF EXIST *x88c48* (GOTO script-a) else goto script-b
IF EXIST *x89c48* (GOTO script-a) else goto script-b
IF EXIST *s20c48* (GOTO script-a) else goto script-b

1 个答案:

答案 0 :(得分:1)

由于您的脚本当前已设置,因此它只会执行第一行。如果你的意思是:

IF EXIST *x71c48* (
    GOTO script-a
) else IF EXIST *x72c48* (
    GOTO script-a
) else IF EXIST *x73c48* (
    GOTO script-a
) else IF...
rem etc. etc. etc.
... else goto script-b

...然后你可以使用findstr正则表达式和条件执行来缩短它。

dir /b | findstr /i "x[0-9][0-9]c[0-9][0-9]" >NUL && goto script-a || goto script-b

您还可以使用for /L循环。

setlocal enabledelayedexpansion
for /L %%I in (0,1,99) do (
    set "int=0%%I"
    if exist *x!int:~-2!c48* goto script-a
)
goto script-b