批处理文件选择命令超过10个参数 [英] Batch file choice command more than 10 parameter

查看:53
本文介绍了批处理文件选择命令超过10个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,该脚本可以找到哪个驱动器承载着我的文件夹(diskpart情况)和创建文件夹,然后它列出了该文件夹中的文本文件(现在是其文本).无论如何,问题在于批处理要求我选择要打开的文件时,它可以工作到9.我什至不能按10,因为当我按1时,它会自动打开文本文件.我该如何写2位数字?因为文件夹总是会更改,所以可能有30个选择.这是我的代码;

I have a batch script which founds which drive is carrying my folder (diskpart situation) and founding folder, then it lists the text files (for now its text) in that folder. Anyways the problem is while the batch is asking me to choose which one do i want to open, it works until 9. I can't even press 10 because when i press 1 it automatically opens the text file. How can i write 2 digit numbers? Because the folder always will change and the might be 30 choices. Here's my code;

@for %%a in ( C D E F G H I J K L M N O P Q R S T U V W X Y Z ) do @if exist %%a:\TAM_IMAGES set The_Drive=%%a
@for %%b in ( C D E F G H I J K L M N O P Q R S T U V W X Y Z ) do @if exist %%b:\Users\canoca\Desktop\bosluktesti set testbasarisiz=%%b
REM @dir %The_Drive%:\TAM_IMAGES
REM @dir %testbasarisiz%:\Users\canoca\Desktop\bosluktesti
cd %The_Drive%:\TAM_IMAGES
setlocal enabledelayedexpansion

set count=0
set "choice_options="

for /F "delims=" %%A in ('dir /a:-d  /b "%The_Drive%:\TAM_IMAGES"') do (

   set /a count+=1

   set "options[!count!]=%%A"

   set choice_options=!choice_options!!count!
)

for /L %%A in (1,1,!count!) do (
   echo %%A]- !options[%%A]!
   
)

choice /c:!choice_options!  /m "Yuklemek istediginiz imaji seciniz: "

REM %testbasarisiz%:\Users\canoca\Desktop\test.bat %The_Drive%:\TAM_IMAGES\!options[%ERRORLEVEL%]!


start %The_Drive%:\TAM_IMAGES\!options[%ERRORLEVEL%]!

cmd /k

推荐答案

您可以使用两个(或多个) choice 命令来模拟两位数的输入(或更多).缺点:您总是必须同时输入两个(全部)数字(如首选 01 ).当然,这意味着您必须手动验证结果输入:

You can emulate two-digit entries (or even more) by using two (or more) choice commands. Downside: You always have to enter both (all) digits ( like 01 for the first choice). Of course, this means you have to validate the resulting input manually:

@echo off
setlocal enabledelayedexpansion

:input
set count=100
for /f "delims=" %%a in ('dir /b /a-d') do (
  set /a count+=1
  echo !count:~-2!] %%a
)
set /a countMax=count-100
set "countNr=%count:~-2%"
<nul set /p "=input (01...%countNr%, 00 for exit): " 
choice /c 1234567890 /n >nul
set first=%errorlevel:~-1%
<nul set /p "=%first%"
choice /c 1234567890 /n >nul
echo %errorlevel:~-1%
set ch=%first%%errorlevel:~-1%
echo that was %ch%
set /a line=1%ch%-101

REM echo debug: ch=%ch%;CountNr=%countNr%;CountMax=%CountMax%,Line=%line%
if "%ch%" == "00" goto escape
if %ch% gtr %countMax% echo bad input&goto :input

for /f "delims=" %%a in ('dir /b /a-d^|more +%line%') do set "file=%%a"&goto :cont
:cont
echo that was %ch% - %file%
echo doing something with %file%.
goto :eof

:escape
echo you choosed '00' for exit.

我假设两次输入都不会改变文件夹的内容,所以通过两次执行 dir 避免了类似数组的变量.

I avoided array-like variables by doing the dir twice, assuming the content of the folder won't change during the input.

这篇关于批处理文件选择命令超过10个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆