Batch file choice command more than 10 parameter

筅森魡賤 提交于 2021-01-01 13:54:04

问题


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

回答1:


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.

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



来源:https://stackoverflow.com/questions/65194566/batch-file-choice-command-more-than-10-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!