.bat - 从文件夹文件列表中创建菜单

Nic*_*ick 1 windows batch-file

我通常不创建.bat文件,但我使这个小脚本对开发很有用.

我正在使用它来读取和创建包含在文件夹中的文件列表:

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

我发现这有关如何从文件列表 - 批处理文件中的多个选择菜单开始创建菜单

现在我的问题是:

我想创建一个菜单,其中包含该文件夹中包含的文件列表,我可以通过按下列表中的相对编号来选择(不是多项选择),但我真的不知道如何合并两位代码以上.

最终结果应该是这样的:

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

select file: 
Run Code Online (Sandbox Code Playgroud)

它将从文件夹中安装所选文件.

任何建议都会非常感激.

提前致谢

Som*_*ark 6

这应该可以工作,除非你使用的是没有的Windows版本choice,比如因为某些原因你还在使用XP.

@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%]!"
Run Code Online (Sandbox Code Playgroud)

  • @equiman - 只要 errorlevel 不在循环或 `if` 语句内更新,当然 (2认同)
  • 非常感谢!对于傻瓜:不要将批处理文件称为“选择”...该死!o_o (2认同)