windowsbatch-file

How to make bat file listing with options to choose


I want to make a bat file that lists all of the files in a specific directory, and adds numbers at the beginning of the every one of the listed items. This numbers need to be a selectable options.

Example: I have a folder with 5 files in it, aaa.exe, bbb.exe, ccc.exe, ddd.exe, eee.exe. When I run bat file I need to see

  1. aaa.exe
  2. bbb.exe
  3. ccc.exe
  4. ddd.exe
  5. eee.exe

So now if I want to run 5-th exe I need to press 5, than press enter and that 5th exe will now start.

I already found how to list all of the items in folder with this code

REM -start "c:\windows\system32" notepad.exe 
for /r %%i in (*) do echo %%i
pause
exit

but I can't figure out how to add numbers in front of the text and make those numbers to be a selectable options.

Edit---

Now I'm getting

ERROR: Duplicate choices are not allowed. running '""' is not recognized as an internal or external command, operable program or batch file.

when I'm trying to run this loop for a second time.

This is code that I wrote:

@ECHO OFF
setlocal enabledelayedexpansion


REM ---Prompt part
:choise
SET /P AREYOUSURE=Install programs (Y/[N])?  
IF /I "%AREYOUSURE%" EQU "Y" GOTO :chooseInstall
IF /I "%AREYOUSURE%" EQU "N" GOTO :nope

REM --Cheking for Y or N
GOTO :choise

:nope
echo "Ok. Have a nice daty / night"
pause
exit



:chooseInstall
echo Which program do you want to install?
echo.
echo 1. 7Zip
echo 2. CPU Z
echo.

SET /P AREYOUSURE=Choosing: 
IF /I "%AREYOUSURE%" EQU "1" set "pathToSoft=C:\Users\usr\Desktop\hello"
IF /I "%AREYOUSURE%" EQU "2" set "pathToSoft=C:\Users\usr\Desktop\bye"


echo.
echo.

echo %pathToSoft%

echo.
echo.


REM ---Installs
echo "Which file to install"

cd %pathToSoft%
echo.
echo.



REM --Loops that scan files

set /A counter=0


for /R %%i in (*) do (

if not "%%~nxi" == "%~nx0" (
    set /A counter+=1
    echo !counter!: %%~nxi
    set exe[!counter!]=%%i
    set choice=!choice!!counter!
    )
)

if %counter% LSS 10 (
choice /C %choice% /M "Choose: "
set EXENUM=!ERRORLEVEL!
) else set /P EXENUM="enter exe number: "


set EXECUTABLE=!exe[%EXENUM%]!
echo running %EXECUTABLE%
call "%EXECUTABLE%"

echo.
echo.
echo.





:installmore
SET /P INSTALLMORE=Do you want to install something else (Y/[N])?  
IF /I "%INSTALLMORE%" EQU "Y" GOTO :chooseInstall
IF /I "%INSTALLMORE%" EQU "N" GOTO :nope

Solution

  • (you have to enable delayedexpansion to be able to use % and ! env. var separators & instant evaluation within the loop)

    can be done like this:

    @echo off
    
    setlocal enabledelayedexpansion
    
    set /A counter=0
    set choice=
    
    for /R %%i in (*) do (
    
    if not "%%~nxi" == "%~nx0" (
    set /A counter+=1
    echo !counter!: %%~nxi
    set exe[!counter!]=%%i
    
    set choice=!choice!!counter!
    )
    )
    
    if %counter% LSS 10 (
    choice /C %choice% /M "type exe number"
    set EXENUM=!ERRORLEVEL!
    ) else set /P EXENUM="enter exe number: "
    
    
    set EXECUTABLE=!exe[%EXENUM%]!
    echo running %EXECUTABLE%
    call "%EXECUTABLE%"