Я хочу рекурсивно искать в папке все exe-файлы, и пакетный сценарий автоматически устанавливает каждый найденный файл program.exe в качестве пользовательского ввода. Он должен автоматически заполнить варианты выбора и остановиться, когда будет найден последний исполняемый файл. Таким образом, если в папке поиска находится в общей сложности 8 exe-файлов, то будет автоматически выведен набор / p на выбор из 8 Programs.exe на выбор. Не 9 вариантов, а не 7. Точное количество вариантов должно быть точным для всех найденных совпадений. Это попытка упростить процесс кодирования.
У меня есть массивный скрипт, который автоматически заполняет до 6 найденных exe-файлов. Здесь лежит моя проблема. Я ограничен. Сценарий хорошо работает для того, что он есть, но я надеюсь, что у кого-то есть хитрость и новый подход к выполнению кода. Я не буду лгать, когда говорю, что этот сценарий, который я сделал, немного беспорядок. Заранее спасибо всем, кто может иметь некоторые идеи
Важное замечание: я скачал и запустил программу под названием cecho.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