1

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

Community
  • 1
  • 1
Nick
  • 13,493
  • 8
  • 51
  • 98
  • Any particular reason you're using `|` as a delimiter? It's impossible for that character to be part of a file name, and if that's the point, then you might as well say `"delims="`. – SomethingDark Jun 02 '15 at 09:41
  • no no particolar reason, i can change it if it helps – Nick Jun 02 '15 at 09:43
  • Also, are you ever going to have more than 10 options? With 10 or fewer, I can throw together a pretty nice `choice` command, but anything after that is going to have a kinda ugly `if` chain. – SomethingDark Jun 02 '15 at 09:43
  • yeah 10 is actually more than fine. it will probably be something like develop - test and release (max 3 release) so I guess 5 or 6, not more – Nick Jun 02 '15 at 10:00

2 Answers2

6

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%]!"
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Just a quick thing, is it possible to list only files (cause i have also 2 folders listed) and have a fixed entry at the bottom of the array? is it easy to do? – Nick Jun 02 '15 at 10:53
  • 1
    @Nick - Sure thing! See my edit. As for the fixed entry at the bottom, just increment !count! after the first `for` loop and then set the options[!count!] and !choice_options! variables accordingly. – SomethingDark Jun 02 '15 at 11:06
  • Sorry i know that you already replied to this, but today i tried again this script and it doesn't work anymore, it says "The system cannot find the file specified" and i don't know why cause there are files. I used the exact code you used in your answer. Can you please help me with this please? thanks – Nick Jun 08 '15 at 11:38
  • @Nick - That could be literally anything, but my best guess upon being given no information at all is that one of the files has a space in the path (don't do that if you can help it, by the way) and so part of the name was getting cut off or something. I've added quotes to the options array, which might help. Of course, if `C:\src\release\android\` has changed and now has spaces, that's a completely different solution altogether. – SomethingDark Jun 22 '16 at 21:09
  • Excelent, thanks! If need know the selected value name ... can do it with `SET project=!options[%errorlevel%]!` – equiman Jul 15 '16 at 18:49
  • 2
    @equiman - as long as errorlevel isn't being updated inside of a loop or an `if` statement, sure – SomethingDark Jul 15 '16 at 18:51
  • 2
    Thanks a lot! For dummies: Don't call the batch file "choice"... damn it! O_o – Marc Nov 30 '17 at 17:10
  • What can I do, if I get more then 9 entries? I get an error like "You can't choose two options" from the choice-command. The `choice_options` contains `12345678910111213141516171819202122` – PeterCo Feb 14 '18 at 09:19
  • 1
    @PeterCo - You're going to need multiple menus at that point. Frankly, having a menu with that many choices is a _huge_ violation of basic user experience principles and you should consider an alternate approach. – SomethingDark Feb 14 '18 at 09:23
  • 1
    [more than 9 entries](https://stackoverflow.com/a/65196145/2152082) – Stephan Sep 29 '21 at 16:30
0

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%]!"
Wil
  • 1