Nick
Nick

Reputation: 14283

.bat - Create a menu from folder file list

I don't usually create .bat file, but I made this little script useful for develop.

I'm using this for reading and creating a list of files contained into a folder:

for /f "delims=|" %%f in ('dir /b C:\src\release\android\') do echo %%f

and I found this about how to create a menu starting from a list of file -> Multiple choices menu on batch file?

Now my question is:

I'd like to create a menu with a list of files contained into that folder which I can select (not multiple selection) by pressing it's relative number on the list, but i don't really know how to merge the two bit of code above.

The final result should work something like:

[1] ..
[2] ..
[3] ..
[4] ..

select file: 

and it will install the selected file from the folder.

Any suggestion would be really appreciated.

Thanks in advance

Upvotes: 1

Views: 3591

Answers (2)

Wil
Wil

Reputation: 1

Improving upon SomethingDark's script to run Python scripts in a user's Document folder (I know, not best practice here for brevity's sake), as it currently wouldn't work when there are more than 10 choices:

@echo off
setlocal enabledelayedexpansion

set count=0
set "choice_options="

for /F "delims=" %%A in ('dir /a:-d /b C:\Users\JohnSmith\Documents\*.py') do (

    REM Increment %count% here so that it doesn't get incremented later
    set /a count+=1

    REM Add the file name to the options array
    set "options[!count!]=%%A"
)

for /L %%A in (1,1,!count!) do echo [%%A]. !options[%%A]!
::prompts user input
set /p filechoice="Enter a file to load: "

:: Location of python.exe and location of python script explicitly stated
echo Running !options[%filechoice%]!...
"C:\Users\JohnSmith\AppData\Local\Microsoft\WindowsApps\python.exe" "C:\Users\JohnSmith\Documents\!options[%filechoice%]!"

Upvotes: 0

SomethingDark
SomethingDark

Reputation: 14305

This should work unless you're using a version of Windows that doesn't have choice, like if you're still on XP for some reason.

@echo off
setlocal enabledelayedexpansion

set count=0
set "choice_options="

for /F "delims=" %%A in ('dir /a:-d /b C:\src\release\android\') do (
    REM Increment %count% here so that it doesn't get incremented later
    set /a count+=1

    REM Add the file name to the options array
    set "options[!count!]=%%A"

    REM Add the new option to the list of existing options
    set choice_options=!choice_options!!count!
)

for /L %%A in (1,1,!count!) do echo [%%A]. !options[%%A]!
choice /c:!choice_options! /n /m "Enter a file to load: "

:: CHOICE selections get set to the system variable %errorlevel%
:: The whole thing is wrapped in quotes to handle file names with spaces in them
:: I'm using type because I'm not familiar with adb, but you be able to get the idea
type "C:\src\release\android\!options[%errorlevel%]!"

Upvotes: 6

Related Questions