如何在不手动编辑脚本的情况下通过找到的文件扩展名自动生成用户输入选择

How to auto generate user input choice by found file extensions without manually editing script

我想递归搜索文件夹中的所有 exe 文件,并让批处理脚本自动将找到的每个 program.exe 文件设置为用户选择输入。它需要适当地自动填充选项并在找到最后一个 exe 时停止。因此,如果搜索到的文件夹中总共有 8 个 exe 文件,那么它会自动输出 set /p choice of 8 programs.exe's 以供选择。不是 9 个选择,也不是 7 个。对于所有找到的匹配项,选择的确切数量需要准确。这是简化编码过程的尝试。

我制作了一个庞大的脚本,可以自动填充最多 6 个找到的 exe 文件。这就是我的问题。我被封顶了。该脚本就其本身而言运行良好,但我希望有人能掌握诀窍并以一种新的方式思考代码执行。当我说我制作的这个脚本有点乱时,我不会撒谎。提前感谢任何可能有想法的人。

重要说明:我下载了名为 cecho.exe 的 exe 程序,它有助于为终端输出文本着色。如果您想调试我的脚本,我强烈建议您使用它。没有它,那将是一团糟。将 cecho.exe 粘贴到您的 system32 文件夹或任何您想要的位置,只要它可以访问而无需文件的完整路径。我在这里找到了程序 (https://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files)

@echo off
if not "%1" == "max" start /max cmd /i /c %0 max & exit/b
setlocal enabledelayedexpansion

set extension=exe
set nt={\n \t}
set ntt={\n \t \t}
set "TAB= "

:: CECHO RED ::
set cred=cecho {0C}
call :cred
exit /b
:cred
setlocal
echo !cred!
endlocal
:: CECHO GREEN ::
set cgreen=cecho {0A}
call :cgreen
exit /b
:cgreen
setlocal
echo !cgreen!
endlocal
:: CECHO YELLOW ::
set cyellow=cecho {0E}
call :cyellow
exit /b
:cyellow
setlocal
echo !cyellow!
endlocal
:: CHOICE ::
set choice=1
call :choice1
exit/b
:choice1
setlocal
echo !choice!
endlocal
goto start

:start
cls
cd "%~dp0"

call :choice1
exit/b
:choice1
setlocal
set choice=!choice!
endlocal
for /r "." %%i in (*.%extension%) do (
set p!choice!=%%~nxi
set /a choice=!choice!+1
)

echo.
call :cyellow
exit /b
:cyellow
setlocal
!cyellow! ...........................................................%ntt2%
!cyellow!     Select a number{\n}
!cyellow! ...........................................................{\n}{#}
endlocal
pause
echo.


set p1=!p1!
call :p1
exit /b
:p1
setlocal
if "%p1%"=="" goto nomatch
if defined p1 (
call :p1
)
:p1
call :cred
call :cgreen
:cred
:cgreen
setlocal
!cred!%ntt%1.
!cgreen! !p1!%ntt%
echo.

set p2=!p2!
call :p2
exit /b
:p2
setlocal
if "%p2%"=="" goto sel_1
if defined p2 (
call :p2
)
:p2
!cred!%ntt%2.
!cgreen! !p2!%ntt%
echo.

set p3=!p3!
call :p3
exit /b
:p3
setlocal
if "%p3%"=="" goto sel_2
if defined p3 (
call :p3
)
:p3
!cred!%ntt%3.
!cgreen! !p3!%ntt%
echo.

set p4=!p4!
call :p4
exit /b
:p4
setlocal
if "%p4%"=="" goto sel_3
if defined p4 (
call :p4
)
:p4
!cred!%ntt%4.
!cgreen! !p4!%ntt%
echo.

set p5=!p5!
call :p5
exit /b
:p5
setlocal
if "%p5%"=="" goto sel_4
if defined p5 (
call :p5
)
:p5
!cred!%ntt%5.
!cgreen! !p5!%ntt%
echo.

set p6=!p6!
call :p6
exit /b
:p6
setlocal
if "%p6%"=="" goto sel_5
if defined p6 (
call :p6
)
:p6
!cred!%ntt%6.
!cgreen! !p6!%ntt%
goto sel_6
echo.

:sel_1
echo.
echo The selected program is !p1!
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==1 goto ex1

:sel_2
echo.
echo sel_2
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==2 goto ex2
if %input%==1 goto ex1

:sel_3
echo.
echo sel_3
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==3 goto ex3
if %input%==2 goto ex2
if %input%==1 goto ex1

:sel_4
echo.
echo sel_4
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==4 goto ex4
if %input%==3 goto ex3
if %input%==2 goto ex2
if %input%==1 goto ex1

:sel_5
echo.
echo sel_5
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==5 goto ex5 
if %input%==4 goto ex4
if %input%==3 goto ex3
if %input%==2 goto ex2
if %input%==1 goto ex1

:sel_6
echo.
set "input="
set /p "input=Select a number or type reset to go to beginning:"
if %input%==reset goto startover
if %input%==6 goto ex6
if %input%==5 goto ex5
if %input%==4 goto ex4
if %input%==3 goto ex3
if %input%==2 goto ex2
if %input%==1 goto ex1

:ex1
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p1!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un1
if %input%==1 goto in1

:ex2
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p2!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un2
if %input%==1 goto in2

:ex3
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p3!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un3
if %input%==1 goto in3

:ex4
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p4!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un4
if %input%==1 goto in4

:ex5
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p5!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un5
if %input%==1 goto in5

:ex6
cls
!cyellow! ...............................................{\n}
!cgreen!%ntt% You have chosen{\n}
!cred!%ntt%   !p6!{\n}
!cyellow! ...............................................{\n}
timeout 1 >nul
echo.
!cyellow! & set "input=Select [1] to Install or Select [2] to Uninstall:"
set /p "input=!input!"
if %input%==2 goto un6
if %input%==1 goto in6

:in1
cls
set p1=!p1!
call :p1
exit /b
:p1
setlocal
timeout 1 >nul
echo.
echo You are about to Install !p1!
pause
set /p verify=Are you sure you want to Install !p1! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p1!
goto end

:in2
cls
echo You are about to Install !p2!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Install !p2! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p2!
goto end

:in3
cls
echo You are about to Install !p3!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Install !p3! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p3!
goto end

:in4
cls
echo You are about to Install !p4!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Install !p4! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p4!
goto end

:in5
cls
echo You are about to Install !p5!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Install !p5! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p5!
goto end

:in6
cls
echo You are about to Install !p6!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Install !p6! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p6!
goto end

:un1
cls
echo You are about to Uninstall !p1!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p1! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p1!
goto end

:un2
cls
echo You are about to Uninstall !p2!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p2! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p2!
goto end

:un3
cls
echo You are about to Uninstall !p3!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p3! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p3!
goto end

:un4
cls
echo You are about to Uninstall !p4!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p4! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p4!
goto end

:un5
cls
echo You are about to Uninstall !p5!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p5! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p5!
goto end

:un6
cls
echo You are about to Uninstall !p6!
timeout 1 >nul
echo.
set /p verify=Are you sure you want to Uninstall !p6! (y/[n])?
if /i "%verify%" neq "y" goto startover
start "" !p6!
goto end

:startover
cls
%cgreen% Restarting script from beginning{\n}
timeout 1 >nul
echo.
%cgreen% Try another option{\n}
timeout 1 >nul
goto start

:nomatch
cls
echo.
%cyellow% ...................................................................................................%ntt%
%cyellow%              The script found no matching extensions{\n}
%cyellow% ...................................................................................................{\n}
timeout 2 >nul
echo.
%cyellow% ...................................................................................................%ntt%
%cyellow%     Make sure to run script in folder with matching extensions{\n}
%cyellow% ...................................................................................................{\n}
timeout 2 >nul
echo.
%cyellow% ...................................................................................................%ntt%
%cyellow%                  Press any key to exit script{\n}
%cyellow% ...................................................................................................{\n}
timeout /t -1 >nul
goto end

:end
endlocal
exit

所以这是一种更自动化的方法,因为您对每个可执行文件所做的几乎相同。

@echo off
setlocal enabledelayedexpansion
set cnt=0
for /r %%i in (*.exe) do (
    set /a cnt+=1
    set "file!cnt!=%%~fi"
    call echo [!cnt!] %%file!cnt!%%
)
set /p "ans=::: Please Select Executable from above list by matching number :::: "
choice /C IUC /M "Do you want to [I]nstall, [U]ninstall '!file%ans%!' or [C]ancel'"
if errorlevel 3 goto :eof
if errorlevel 2 (
    choice /C YN /M "Are you sure you want to Uninstall '!file%ans%!'"
    if errorlevel 2 goto :eof
    if errorlevel 1 echo start "" "!file%ans%!" Uninstall procedure here
    goto :eof
)
if errorlevel 1 (
    choice /C YN /M "Are you sure you want to Install '!file%ans%!'"
    if errorlevel 2 goto :eof
    if errorlevel 1 echo start "" "!file%ans%!" Install procedure here
    goto :eof
)

它会简单列出每个可执行文件,你select它对应的数字,它会要求install/uninstall并验证一次,那么在这种情况下它会回显install/uninstall程序。你只需要修改它来添加你的实际程序。我正在向你展示这个想法,你必须扩展它以满足你的需要:)