1

All scripts that i looked were same, they are listing the files in a folder and asking me to choose. And when i press for example "3" nothing is happening. I want batch to LIST files in a folder, then ASKS me to choose one, and the most important part: OPEN what i choosed

@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

Please help me to OPEN that text file, im stuck.. Thanks for every help

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
cankut
  • 19
  • 7
  • You need to add something for it to do when the choice is made! I'd suggest you put that somewhere between line `20` and line `22` – Compo Dec 06 '20 at 19:30

1 Answers1

0

you can add a line:

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

in full:

@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

lww
  • 624
  • 1
  • 7
  • 14
  • Omg thank you A LOT. It worked im really grateful.Can i ask some explanation about what is !options[%ERRORLEVEL%]! – cankut Dec 06 '20 at 21:12
  • For example, if you have three files a.jpg b.jpg c.jpg in a directory C:\image\Aselsan\ !options[%%A]! in the for loop is an array of file names contained in this directory with corresponding indices: `[1]. a.jpg [2]. b.jpg [3]. c.jpg` The choice command sets ERRORLEVEL environment variable to the index of the key that the user selects from the list of choices (according to the Microsoft documentation). Let's say I select file b.jpg by typing 2, then ERRORLEVEL is set to 2. it expands to `start C:\image\Aselsan\!options[2]!` it works because 2 is the index of the file b.jpg – lww Dec 06 '20 at 22:34
  • Thanks a lot <33 – cankut Dec 07 '20 at 20:22
  • hi can you help this? https://stackoverflow.com/questions/65194566/batch-file-choice-command-more-than-10-parameter – cankut Dec 08 '20 at 07:16
  • Hi, it looks like I am late, Stephan has already answered this question. – lww Dec 08 '20 at 11:28
  • 1
    no reason to not add another answer, when it's better or different `;)` – Stephan Dec 08 '20 at 11:32
  • 1
    I think your solution is pretty cool; I'm saving your answer to my private gists. :) – lww Dec 08 '20 at 12:15
  • It's creating a file named "0". It would be better if you save it after we fix this problem ( asked it now ) – cankut Dec 08 '20 at 12:25