Give this one a spin. It uses macros to handle the definition of arrays, Dynamic menus and file selection.
@Echo Off & Setlocal DisableDelayedExpansion
::: ************************************************************** || Macro Definitions
::: Delayed expansion must be enabled AFTER definition of macros, and BEFORE their use.
Set "TAB= "
Set "Menu=Echo/!DIV!&For %%n in (1 2)Do if %%n==2 ((Set "CHCS="&For %%G in (!Options!)Do (Set "Opt=%%~G"& Set "CHCS=!CHCS!!Opt:~0,1!"&Set "Opt[!Opt:~0,1!]=%%~G"& Set "Opt=[!Opt:~0,1!]!Opt:~1!"& Echo/!Opt!))&Echo/!DIV!& For /F "Delims=" %%o in ('Choice /N /C:!CHCS!')Do (For %%C in (!Opt[%%o]!)Do (Set "OPTION=%%C")))Else Set Options="
Set "DEF/array=(If "!#$E!"=="" (Set "#$E=-1"))&For %%n in (1 2)Do if %%n==2 (Set /A "#$E+=1"&Set "$E=!$E!"&For %%G in (!$E!)Do (Set "$E[!#$E!]=%%~G"))Else Set $E="
rem // extension type provided using Substring modification to replace $E
Set "SelectFile=(If Not !#$E! GTR 0 (Echo/No $E Files found. & Exit /B 0))& (For /L %%i in (0 1 !#$E!) Do If Not "!$E[%%i]!"=="" Echo/%%i:!TAB!!$E[%%i]!)&Echo/Select a $E file number [0-!#$E!]:&Set /P "FN=FN: "&For %%v in ("!FN!")do (If /I "!FN!"=="Exit" (Exit /B 0)Else If not "!$E[%%~v]!"=="" (Set "PATH[$E]=!$E[%%~v]!"&Echo/!$E[%%~v]! Selected)Else (Goto :$E))"
rem // selected path returned in !PATH[ext]!
::: ************************************************************** || End Macro Definitions
Setlocal EnableExtensions EnableDelayedExpansion
::: Build Dividing line based on actual console dimensions.
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do Set "Console_Width=%%%W"
Set "DIV="&For /L %%i in (2 1 %Console_Width%)Do Set "DIV=!DIV!-"
::: ************************************************************** || Script Body
Set "SourceDir=%~dp0"
PUSHD "%SourceDir%"
For %%O in (*.mkv)Do %DEF/array:$E=mkv%"%%~fO"
For %%O in (*.srt)Do %DEF/array:$E=srt%"%%~fO"
:menu
cls & Echo/.mkv Files:!#mkv! .srt Files: !#srt!
%Menu%mkv Srt Exit
If /I "!Option!"=="Exit" (POPD & Endlocal & Endlocal & Goto :Eof) Else Call :!Option!
PAUSE
Goto :menu
:mkv
%SelectFile:$E=mkv%
Exit /B 0
:srt
%SelectFile:$E=srt%
Exit /B 0
Invalid input is flagged as the input is used to index the array element. False input results in an empty element which triggers a return to the extension label the selectFile macro is provided with at expansion via substring modification.
The breakdowns:
---------------------------------------------------------------------------------------
- Menu Macro:
- Display Dividing line with value in DIV variable and...
- Use If else outer for loop to capture, modify and display the strings to be used for menu options.
- The first character of each string is used as the CHOICE character for user selection
- Will fail if multiple strings containing the same first character are supplied.
- Assigns each string to an indexed value that is used to return the selected string in the OPTION variable
- using the output of the Choice command which is captured using the For /F %%o loop.
Echo/!DIV! & For %%n in (1 2)Do if %%n==2 (
Set "CHCS="
For %%G in (!Options!)Do (
Set "Opt=%%~G"
Set "CHCS=!CHCS!!Opt:~0,1!"
Set "Opt[!Opt:~0,1!]=%%~G"
Set "Opt=[!Opt:~0,1!]!Opt:~1!"
Echo/!Opt!
)
Echo/!DIV!
For /F "Delims=" %%o in ('Choice /N /C:!CHCS!')Do For %%C in (!Opt[%%o]!)Do Set "OPTION=%%C"
)Else Set Options=
---------------------------------------------------------------------------------------
- DEF/Array macro:
- Expanded with substring modification, $E is replaced the the variable name to be used for a given array
- Initial If condition Prepares an undefined Array for incrementation from a 0 index
- Array element is captured using the If / Else outer for loop and assigned to the incremented index value in the array
(If "!#$E!"=="" (Set "#$E=-1")) & For %%n in (1 2)Do if %%n==2 (
Set /A "#$E+=1"
For %%G in (!$E!)Do Set "$E[!#$E!]=%%~G"
)Else Set $E=
---------------------------------------------------------------------------------------
- SelectFile macro:
- Tests the index value of array for a positive file count, returns to previous menu if fails
- Iterates over array for the variable substitued with $E during expansio from 0 index and outputs defined files
- and their index number for selection. Array name is shared with the relevant label to allow return to the correct label when
- invalid input is supplied via Set /P for indexed file selection
- Assigns filepaths to variable Path[$E], again, $E is replaced with the relevant file type via substring modification during expansion.
If Not !#$E! GTR 0 (Echo/No $E Files found. & Exit /B 0)
For /L %%i in (0 1 !#$E!) Do If Not "!$E[%%i]!"=="" Echo/%%i:!TAB!!$E[%%i]!
Echo/Select a $E file number [0-!#$E!]:&Set /P "FN=FN: "&For %%v in ("!FN!")do (
If /I "!FN!"=="Exit" (Exit /B 0)Else If not "!$E[%%~v]!"=="" (
Set "PATH[$E]=!$E[%%~v]!"
Echo/!$E[%%~v]! Selected
)Else (Goto :$E)
)
Note: code supplied for breakdowns is non-functional, it's just been deconstructed to allow you to more easily see
the steps each macro takes to achieve the outcomes.