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

时间:2015-06-02 09:29:57

标签: windows batch-file

我通常不会创建.bat文件,但我将这个小脚本用于开发。

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

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

我找到了关于如何从文件列表开始创建菜单的信息 - > Multiple choices menu on batch file?

现在我的问题是:

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

最终结果应该是这样的:

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

select file: 

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

任何建议都会非常感激。

提前致谢

1 个答案:

答案 0 :(得分:6)

这应该有效,除非您使用的是没有choice的Windows版本,就像您出于某种原因仍在使用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%]!"