0

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
dan1st
  • 12,568
  • 8
  • 34
  • 67
cankut
  • 19
  • 7
  • You will have to use `SET /P` to accept the user input. – Squashman Dec 08 '20 at 07:14
  • I can input until 9th parameter, the problem is i can only input 1 digit. After that it automatically opens the file – cankut Dec 08 '20 at 07:18
  • See my answer [here](https://stackoverflow.com/a/61823099/12343998) for a method that uses a count and substring modification to build choice menus of up to 39 items – T3RR0R Dec 08 '20 at 08:55

1 Answers1

3

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.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks for the nice answer, now i can write 2 digits but there is one problem and i have two question. The problem is batch is creating a file named "0" and i don't want that. My questions are; why did we set count to 100 and how will i write the name of the file that i choose ( i want to write at the end: start C:\users\hp\"the file i choose" ) – cankut Dec 08 '20 at 12:17
  • 1
    We need two-digit numbers (with a leading zero for numbers < 10) Therefore I start with `100` and later take only the last two digits (easier than adding the zero *if* <10). – Stephan Dec 08 '20 at 12:36
  • 1
    `the file i choose` is in the variable `%file%`, so your command would be `start "" "C:\users\hp\the file i choose"` (note the `""` - `start` takes the first quoted argument as a windowtitle, but we need to quote `C:\users\hp\%file%` to make sure it works with spaces in the filename. So we give it an empty "fake" argument as a title (not used, but necessary for proper syntax)) – Stephan Dec 08 '20 at 12:36
  • Is there a way to use backspace to delete an input ( for misclicking situations ) and using enter? – cankut Dec 10 '20 at 13:08
  • Not with `choice`. It doesn't take keys, but characters. Backspace and Enter are keys. If you want "edit capabilities" during input, you have to use `set /p` - but that also means you have to do a complete input verification yourself. And there is [that strange xcopy-trick](https://www.dostips.com/forum/viewtopic.php?t=718), which should be adaptable when you accept some headache (and it doesn't release you from the mentioned input verification) – Stephan Dec 10 '20 at 18:05
  • I've just noticed i explained myself wrongly. I don't want "Enter" as a choice, my choices will be numbers but i don't want batch to take inputs automatically. I want it to wait for my enter command, for example if i press 0 and 2 nothing happens, if i press 0,2 and enter then it opens what's in 02. Is this possible? – cankut Jan 01 '21 at 13:18