简体   繁体   English

批处理文件循环问题

[英]Batch file loop issues

I am having loop issues in my batch file. 我的批处理文件中存在循环问题。 When using the set command, I give the user two choices. 使用set命令时,我给用户两个选择。 The first option works fine however the second option is ignored unless a loop function is done to re-ask the question. 第一个选项工作正常,但第二个选项被忽略,除非进行循环功能重新提问。 Then it accepts the input for the second option. 然后它接受第二个选项的输入。

:WithAccess
cls
If not exist "C:\Program Files (x86)\Microsoft Office\Office15\Winword.exe" (
    goto UninstallViewers
    :WithAccess2
    echo Installing MS Office 2013 x32 With Access...
    start /wait "" "MS Office 2013 x32 Installers\MSOfficeWithAccess"
    echo Installation Complete.
) Else (
    echo Microsoft Office 2013 might already be installed.
    set /p op4=Do you want to run installer anyway? [Y/N]:
    If "%op4%"=="y" goto WithAccess2
    If "%op4%"=="n" goto end
    echo That's not a valid option.
    goto WithAccess
)

So, in the "Else" part of this if statement, when the user chooses option 2 or "n" it will ignore this and move onto whatever code is after it. 因此,在这个if语句的“Else”部分中,当用户选择选项2或“n”时,它将忽略它并移动到它之后的任何代码。 In this case it loops back to the beginning of this code segment. 在这种情况下,它循环回到此代码段的开头。 However after the loop, it now accept the user input and correctly jumps to the "end" label. 但是在循环之后,它现在接受用户输入并正确跳转到“结束”标签。

After playing around I found that removing the labels surrounding this segment fixes it, but that makes it so if the user makes an error, it wont loop back and re-ask the question. 在玩完之后我发现删除这个段周围的标签可以修复它,但是如果用户发出错误就会这样做,它就不会循环回来并重新提问。 Originally my "Else" statement looked like this: 最初我的“Else”声明看起来像这样:

Else (
    echo Microsoft Office 2013 might already be installed.
    :Loop2
    set /p op4=Do you want to run installer anyway? [Y/N]:
    If "%op4%"=="y" goto WithAccess2
    If "%op4%"=="n" goto end
    echo That's not a valid option.
    goto Loop2
)

So that it would ideally loop the same question but obviously the same issue occurred. 因此理想情况下循环相同的问题,但显然发生了同样的问题。 I've tried searching around for the answer but its hard to search for such a specific coding issue. 我试图寻找答案,但很难找到这样一个特定的编码问题。 Any help would be appreciated. 任何帮助,将不胜感激。

It's bad practice to put labels within parenthetical code blocks. 将标签放在括号内的代码块中是不好的做法 If you goto label and that label is within a code block, the thread of execution lands on the label thinking it is no longer within a code block, and problems arise. 如果goto label并且该标签位于代码块内,则执行的线程会落在标签上,认为它不再位于代码块内,并且会出现问题。 You should re-work the logic of your script's flow. 您应该重新编写脚本流程的逻辑。 Move your labels outside any parentheses, and consider using call rather than goto when you want to call a function then return to continue parsing the next line. 将标签移到任何括号之外,当你想调用一个函数然后返回继续解析下一行时,考虑使用call而不是goto You might even find it useful to return values from your functions from time to time. 您甚至可能会发现从函数中不时返回值很有用。 Also, use if /i for case insensitive tests. 另外,使用if /i进行不区分大小写的测试。

:WithAccess
cls
If not exist "C:\Program Files (x86)\Microsoft Office\Office15\Winword.exe" (
    call :UninstallViewers
) Else (
    echo Microsoft Office 2013 might already be installed.
    set /p "op4=Do you want to run installer anyway? [Y/N]: "
    setlocal enabledelayedexpansion
    If /I "!op4!"=="y" goto WithAccess2
    If /I "!op4!"=="n" goto :EOF
    endlocal
    echo That's not a valid option.
    goto WithAccess
)

:WithAccess2
echo Installing MS Office 2013 x32 With Access...
start /wait "" "MS Office 2013 x32 Installers\MSOfficeWithAccess"
echo Installation Complete.

:: end main runtime
goto :EOF

:UninstallViewers
:: (or whatever code you have to do the uninstalling)
wmic product where "name like '%%viewer%%' and vendor like '%%microsoft%%'" call uninstall
goto :EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM