【问题标题】:Create a batch file with multiple options创建具有多个选项的批处理文件
【发布时间】:2012-02-16 07:56:40
【问题描述】:

您好,我想创建一个batch,它执行多个操作。喜欢它应该显示

1.Restart
2.Shutdown
3.Close all Windows
4.Log off
5.Switch User

然后“Enter your choice:(User Choice) 然后它应该执行操作

你能帮忙吗?

【问题讨论】:

  • 这是你家庭作业的一部分吗?你做了什么 ?你被困在哪里了?
  • 我可以得到用户的选择,比如 set /p task=Enter your Choice:%=%.我想在“输入您的选择:”之前包含可用的用户选项:

标签: batch-file cmd


【解决方案1】:

这应该可以帮助您入门。 CHOICE 命令在大多数版本的 Windows 中都可用,但可能需要您获得 Windows NT 4 资源工具包。 CHOICE 命令在 Windows 7 中可用。

@ECHO OFF
CLS
ECHO 1.Restart
ECHO 2.Shutdown
ECHO 3.Close all Windows
ECHO 4.Log off
ECHO 5.Switch User
ECHO.

CHOICE /C 12345 /M "Enter your choice:"

:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 5 GOTO SwitchUser
IF ERRORLEVEL 4 GOTO Logoff
IF ERRORLEVEL 3 GOTO CloseAllWindows
IF ERRORLEVEL 2 GOTO Shutdown
IF ERRORLEVEL 1 GOTO Restart

:Restart
ECHO Restart (put your restart code here)
GOTO End

:Shutdown
ECHO Shutdown (put your shutdown code here)
GOTO End

:CloseAllWindows
ECHO Close All Windows (put your close all windows code here)
GOTO End

:Logoff
ECHO Logoff (put your log off code here)
GOTO End

:SwitchUser
ECHO Switch User (put your switch user code here)
GOTO End

:End

【讨论】:

  • 感谢小伙伴们的宝贵回答
  • 另外,为了完整起见,您可能应该在顶部的 echo 部分添加 option1
  • 在某些情况下,逻辑需要使用带有相等运算符的环境变量:IF %ERRORLEVEL% == 5 GOTO SwitchUser。
【解决方案2】:

现在有人刚刚放了这是你的作业...我几乎不想粘贴这个!哈哈

但是...无论如何我都会:p

echo off
:begin
echo Select a task:
echo =============
echo -
echo 1) Option 1
echo 2) Option 2
echo 3) Option 3
echo 4) Option 4
echo -
set /p op=Type option:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3
if "%op%"=="4" goto op4

echo Please Pick an option:
goto begin


:op1
echo you picked option 1
goto begin

:op2
echo you picked option 2
goto begin

:op3
echo you picked option 3
goto begin

:op4
echo you picked option 4
goto begin

:exit
@exit

如果您希望它运行命令然后关闭批处理文件,您可以在每个部分中将 goto begin 交换为 goto exit,这是我的建议,因为您想关闭。

希望这会有所帮助。

马丁

【讨论】:

    【解决方案3】:

    这样更好……

    @echo off 
    title A better way
    color 0a
    echo.
    echo 1.restart
    echo 2.shutdown
    echo 3.close all windows
    echo 4.log off
    echo 5.switch user
    echo.
    
    set /p a=
    IF %a%==1 (put in restart code)
    IF %a%==2 (put in shutdown code)
    IF %a%==3 (put in close all Windows code)
    IF %a%==4 (put in log out code)
    IF %a%==5 (put in switch user code)
    

    【讨论】:

      【解决方案4】:

      网上有很多关于批处理编程的教程,比如http://www.infionline.net/~wtnewton/batch/batguide.html。你可以去看看

      命令 1.Restart 2.Shutdown 3.Close all Windows 4.Log off 5.Switch 用户可以从命令执行中获得,但需要管理员权限。 你可以试试这个以获得可用的选项。

      shutdown help
      

      在命令行上。

      【讨论】:

        【解决方案5】:

        此脚本具有所有常见的关闭命令行选项。选择您的选择并享受。

        @echo off
        setlocal
        
        title Shutdown Script
        color 0A
        
        set seconds=1
        
        :start
        cls
        echo.
        echo Select a number:
        echo.
        echo [1] Restart (Default Setting)
        echo.
        echo [2] Restart Reregister Applications
        echo.
        echo [3] Restart UEFI/BIOS
        echo.
        echo [4] Restart Advanced Boot Options
        echo.
        echo [5] Shutdown (Default Setting)
        echo.
        echo [6] Shutdown Reregister Applications
        echo.
        echo [7] Sign Out User
        echo.
        echo [8] Exit
        echo.
        
        choice /c 12345678 /m "Enter your choice:"
        if errorlevel 8 goto :end
        if errorlevel 7 (
        cls
        echo.
        echo Sign out
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /l
        goto error
        )
        
        if errorlevel 6 (
        cls
        echo.
        echo Shutdown PC and Re-register any applications on next boot
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /sg /t %seconds%
        goto error
        )
        
        if errorlevel 5 (
        cls
        echo.
        echo Shutdown PC ^(Default Setting^)
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /s /t %seconds%
        goto error
        )
        
        if errorlevel 4 (
        cls
        echo.
        echo Restart PC and load the advanced boot options menu
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /r /o /t %seconds%
        goto error
        )
        
        if errorlevel 3 (
        cls
        echo.
        echo Restart PC to UEFI/BIOS menu
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /r /fw /t %seconds%
        goto error
        )
        
        if errorlevel 2 (
        cls
        echo.
        echo Restart PC and Re-register any applications
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /g /t %seconds%
        goto error
        )
        
        if errorlevel 1 (
        cls
        echo.
        echo Restart PC ^(Default Setting^)
        choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
        if errorlevel 3 goto end
        if errorlevel 2 goto startover
        if errorlevel 1 shutdown /r /t %seconds%
        goto error
        )
        
        :startover
        cls
        echo.
        echo Restarting script
        timeout 2 >nul
        goto start
        
        :error
        cls
        echo.
        echo You might be here because of a bad input selection
        timeout 2 >nul
        echo.
        echo Perhaps try another input
        endlocal
        exit /b
        
        :end
        cls
        echo.
        echo Exiting
        timeout 2 >nul
        endlocal
        exit /b
        

        【讨论】:

          猜你喜欢
          • 2019-07-20
          • 2020-04-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 2016-02-26
          • 2021-12-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多