避免批处理文件中的长代码行

avoid long lines of code in batch file

应该很简单。我制作了一批有效的,但我认为它包含过长的句子。我不得不玩弄括号,但很快意识到我不明白自己在做什么。任何提示都会很棒。

这是代码

IF "%deletetype%" == "prompt" (for /d /r "C:\Users" %%a in (*Folder*) do if exist "%%a" rmdir /s "%%a") ELSE (for /d /r "C:\Users" %%a in (*Folder*) do if exist "%%a" rmdir /s /q "%%a")
IF "%deletetype%" == "prompt" (for /d /r "C:\Documents and Settings" %%a in (*Folder*) do if exist "%%a" rmdir /s "%%a") ELSE (for /d /r "C:\Documents and Settings" %%a in (*Folder*) do if exist "%%a" rmdir /s /q "%%a")

如您所见,它似乎太长了,一旦我将 ELSE 移动到任何地方或将其括起来,代码就会中断,我被卡住了。我能做些什么来打破它 分成更小的块。谢谢!

在此感谢所有帮助。我想知道我是否可以将其应用于其他也可以缩短的句子。我尝试使用下面的代码,但我又超出了我的理解范围。我想知道你是否不能使用 || 来做到这一点(运行 如果失败) && (运行 如果成功) 字符?也许使用不同的护发素?

msiexec.exe /norestart /qn /x {3EBE26E0-8630-4351-B18F-06AB4067AC49} || Echo Driver Already Removed or Not Exist! && (pause) && If "%drvtype%" == "grundig" (goto drivers)

设法做到了...谢谢!!

msiexec.exe /norestart /qn /x {07A1C1F1-52A2-49F4-A413-FF9A4FC3EDB6} || ( 
Echo   Olympus Driver Already Removed or Not Exist! Press Space Bar to Continue 
) && pause>nul && (If "%drvtype%" == "olympus" (goto drivers) 
    )
)

您可以这样编写脚本:

IF "%deletetype%" == "prompt" (
    for /d /r "C:\test" %%a in (*Folder*) do (
        if exist "%%a" rmdir /s "%%a"
    )
) ELSE (
    for /d /r "C:\test" %%a in (*Folder*) do (
        if exist "%%a" rmdir /s /q "%%a"
    )
)

此方法适用于 if-else 语句的关键是将 ELSE 与 IF 的右括号放在同一行。