Bat - 脚本 - If + 循环 + 数组(i 和 j) - 显示空白 cmd 窗口而不是运行程序 |我想保留 i 和 j 循环

问题描述 投票:0回答:1

打开空的 cmd 窗口而不是启动 cmd 命令

`@echo off
REM The echo off and other things don't work with power shell, only with cmd, another reason why wasn't working
setlocal enabledelayedexpansion

set FileTasks[0]=1 - Open task manager.
set FileTasks[1]=2 - Open event viewer.
set FileTasks[2]=3 - Open resources monitoring.
set FileTasks[3]=4 - Open performance monitoring.

set Task[0]=taskmgr
set Task[1]=eventvwr
set Task[2]=resmon
set Task[3]=perfmon

REM Main Script
echo Good morning, what is your name?
set /p Input_name=Name:
echo Hey %Input_name%, my name is bat the helper, here are the following 4 tasks I am able to execute if you wish.
echo Please type 1 - 4 to choose the wanted task:

REM Beginning of Function
:show_options_tasks
for /L %%i in (0,1,3) do (
    echo !FileTasks[%%i]!
REM The "!" is responsible to let the system get the values from the loop, that is why wasn't working before, differently from other  programming languages. Also keep the "%% within the brackets with the i"
)
REM End of Function

set /p Input_choice=Answer:

REM Beginning of Function
:first_interaction_complete_task
REM Selection of desired task
for /L %%i in (1,1,4) do (
    if "!Input_choice!" == "!%%i!" (
        for /L %%j in (0,1,0) do (
            echo Thanks, you should see the requested program open in a new window!
            start !Task[%%i-1]!
            timeout /t 10 /nobreak > nul
            
        )
    )
)
REM End of Function
exit /b`

打开空的 cmd 窗口而不是启动 cmd 命令。如果可能的话,我想保留循环,但也对不同的方法持开放态度。

arrays loops if-statement batch-file cmd
1个回答
0
投票

也许像这样:

@echo off
echo Good morning, what is your name?
set /p name=Name:
set Tasks= "task manager" "event viewer" "resources monitoring" "performance monitoring" ^
"CommandLine" "CommandLine New Window" "Batch Helper"
set Comms="taskmgr" "eventvwr" "resmon" "perfmon" "cmd /k" "start cmd" "Help"
setlocal enabledelayedexpansion

:Demo
CLS
CALL :MENU %Tasks%
CALL :COMMANDS %Comms%
CALL :CHOSE
if "%Command%"=="Help" (call :Helper) else (%Command%)
Goto :Demo

:Helper
echo Hey %name%, my name is bat the helper.
echo Here are the following I am able to execute if you wish.
echo Please type number to choose the wanted task.
rem if want to exit from "cmd /k" menu, just type "exit" manually and will back to main menu
pause& exit /b

================================================================================================
REM @squashman : https://stackoverflow.com/users/1417694/squashman
:MENU
set /a n=0& set choices=
FOR %%I IN (%*) do (set /a n+=1& call :ResolveChar !n!& echo [!retval!] %%~I)
GOTO :EOF
:COMMANDS
set /a N=0& set choices=
FOR %%I IN (%Comms%) do (set /a N+=1& call :ResolveChar !N!& ^
set Options!retval!=%%~I& set choices=!choices!!retval!)
GOTO :EOF
:ResolveChar
SETLOCAL
set keys=_1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
set _startchar=%1
CALL SET char=%%keys:~%_startchar%,1%%
ENDLOCAL & SET retval=%char%
goto :eof
:CHOSE
choice /c !choices! /m "Choose: " &rem /n
set num=%errorlevel%
call :ResolveChar %errorlevel%
set Command=!Options%retval%!
rem echo Choosen: [%retval%:%num%] %Command%
GOTO :EOF

输出:

[1] task manager
[2] event viewer
[3] resources monitoring
[4] performance monitoring
[5] CommandLine
[6] CommandLine New Window
[7] Batch Helper
Choose: [1,2,3,4,5,6,7]?
© www.soinside.com 2019 - 2024. All rights reserved.