How do I condense this?

坚强是说给别人听的谎言 提交于 2019-12-24 10:39:05

问题


It's actually much longer than this. What is the better way to do this? I'm trying to write a decider that looks at files and determines which script to goto.

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:


As your script is currently set, it will only execute the first line anyway. If you meant:

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

... then you could shorten it with a findstr regular expression and conditional execution.

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

You could also use a for /L loop.

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


来源:https://stackoverflow.com/questions/29303906/how-do-i-condense-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!