将Batch If / ELSE语句分成多行-难以阅读的代码

时间:2019-05-08 20:15:39

标签: batch-file syntax-error

我在这里找到了此代码-> :: Check if the path is File or Folder using batch

但是我发现很难阅读并且想要简化它/将其分解。我不确定如何执行此操作,我尝试了以下操作,但进行了一些更改,均无济于事。有人可以帮忙吗?谢谢!

此代码有效:

   select * from product inner join variation  on product.id = variaction.product_id where product.code = "code" or variation.code = "code".

这是我想要的样子,但无法弄清楚:

@Echo Off
Set "ATTR=D:\Download\Documents\New"
For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" (Echo Directory
) Else If "%%~aZ" GEq "-" (Echo File) Else Echo Inaccessible
Pause

2 个答案:

答案 0 :(得分:1)

在链接中获取初始代码,并添加括号有助于破坏IfElse结构:

@Echo Off
For %%Z In ("%ATTR%") Do (
    If "%%~aZ" GEq "d" (
        Echo Directory
    ) Else (
        If "%%~aZ" GEq "-" (
            Echo File
        ) Else (
            Echo Inaccessible
        )
    )
)
Pause

因此,使用GoTo对其进行修改,也许像这样更适合您的目的:

@Echo Off
PushD "%~dp0"
ClS
Set "ATTR=%AppData%\Microsoft\Excel\XLSTART"
For %%Z In ("%ATTR%") Do (
    If "%%~aZ" GEq "d" (
        GoTo DIR
    ) Else (
        If "%%~aZ" GEq "-" (
            GoTo FILE
        ) Else (
            GoTo NOACCESS
        )
    )
)
GoTo NOTFOUND

:DIR
Echo "Directory Found!"
GoTo ENDFOR

:FILE
Echo "File Found!"
GoTo ENDFOR

:NOTFOUND
Echo "Not Found!"
GoTo ENDFOR

:NOACCESS
Echo "Inaccessible!"

:ENDFOR
Pause
Exit /B

答案 1 :(得分:0)

通过将GoTo Foo放在方括号中并固定一行,使我更容易阅读,因为“ ELSE”不能单行。

@echo off
pushd %~dp0
cls
::https://stackoverflow.com/questions/47954081/check-if-the-path-is-file-or-folder-using-batch 

set "ATTR=%AppData%\Microsoft\Excel\XLSTART"

::For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" (Echo Directory
::) Else If "%%~aZ" GEq "-" (Echo File) Else Echo Inaccessible
::Pause

For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" (GoTo DIR) Else If "%%~aZ" GEq "-" (GoTo FILE) Else (GoTo NOTFOUND)

:DIR
Echo "Dir Found!"
Pause
GoTo ENDFOR

:FILE
Echo "File Found!"
Pause
GoTo ENDFOR

:NOTFOUND
Echo "NOTHING FOUND!"
Pause
GoTo ENDFOR

:ENDFOR