Search code examples
listbatch-filecmd

Batch will list files in a folder, ask me which one and when i choose it will open it


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


Solution

  • 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