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

标签 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: 

它将从文件夹中安装选定的文件。

如有任何建议,我们将不胜感激。

提前致谢

最佳答案

这应该有效,除非您使用的 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%]!"

关于windows - .bat - 从文件夹文件列表创建菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592253/

相关文章:

windows - 如何理解批处理中的表达式 "%PATH:;= %"

c# - CMD.exe C# 模拟器?

c++ - Windows 上的 TLS session 恢复

java - Windows中执行计划任务时如何隐藏svchost.exe(dos提示符)

php - File_get_contents 函数不适用于 Windows?

matlab - 使用 Matlab 将日志语句写入标准输出

string - 如何对字符串列表进行批量排序?

c# - 如何显示 Windows 登录对话框?

Android Studio 无法检测到 appt,即使它位于错误指定的位置

security - 这是批处理文件注入(inject)吗?