【问题标题】:Batch will list files in a folder, ask me which one and when i choose it will open it批处理将列出文件夹中的文件,问我是哪一个,当我选择它时会打开它
【发布时间】:2020-12-06 18:34:59
【问题描述】:

我查看的所有脚本都是相同的,它们列出文件夹中的文件并要求我选择。当我按下例如“3”时,什么也没有发生。我想批量列出文件夹中的文件,然后要求我选择一个,最重要的部分:打开我选择的文件

@echo off
setlocal enabledelayedexpansion

set count=0
set "choice_options="

for /F "delims=" %%A in ('dir /a:-d /b C:\image\Aselsan\') 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: "

cmd /k

请帮我打开那个文本文件,我卡住了。。谢谢大家的帮助

【问题讨论】:

  • 您需要在做出选择时为其添加一些内容!我建议你把它放在2022 之间的某个地方

标签: list batch-file cmd choice


【解决方案1】:

你可以添加一行:

start C:\image\Aselsan\!options[%ERRORLEVEL%]!

全文:

@echo off
setlocal enabledelayedexpansion

set count=0
set "choice_options="

for /F "delims=" %%A in ('dir /a:-d  /b "C:\image\Aselsan\"') 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]!
    echo %%A
   
)

choice /c:!choice_options!  /m "Enter a file to load: "

start C:\image\Aselsan\!options[%ERRORLEVEL%]!

cmd /k

【讨论】:

  • 天哪,非常感谢。我真的很感激。我可以问一些关于什么是 !options[%ERRORLEVEL%] 的解释!
  • 例如,如果您在目录 C:\image\Aselsan\ 中有三个文件 a.jpg b.jpg c.jpg !options[%%A]!在 for 循环中是包含在此目录中的文件名数组以及相应的索引:[1]. a.jpg [2]. b.jpg [3]. c.jpgchoice 命令将 ERRORLEVEL 环境变量设置为用户从选项列表中选择的键的索引(根据 Microsoft 文档) .假设我通过键入 2 选择文件 b.jpg,然后 ERRORLEVEL 设置为 2。它扩展为 start C:\image\Aselsan\!options[2]! 它可以工作,因为 2 是文件 b.jpg 的索引
  • 非常感谢
  • 没有理由不添加另一个答案,当它更好或不同时;)
  • 我认为你的解决方案很酷;我将您的答案保存到我的私人要点中。 :)
猜你喜欢
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多