Search code examples
batch-filecmdmultiple-select

How to list all files in folder then select multiple files


I would like to be able to:

  1. Put my .bat script in some folder with files and run it
  2. In CMD, script should list all files in that folder with select-able numbers
  3. In user prompt, user should be able to choose two or more files for further processing, by entering their numbers separated by commas, spaces or whatever

Below is what I have so far. It can choose one file only. I am not developer, this was copy/pasted/edited and cannot figure out how to enhance it to work with multiple files. The final goal is to have names of chosen files in .txt file, one name per new line

Many thanks in advance.

echo off
:START
cls
cls
setlocal enabledelayedexpansion
set count=0
:
:: Read in files
for %%x in (*.*) do (
  set /a count=count+1
  set choice[!count!]=%%x
)

:
echo.
echo Please choose file by number:
echo.

:
:: Print list of files
for /l %%x in (1,1,!count!) do (
   echo %%x] !choice[%%x]!
)
echo.

:
:: Retrieve User input
set /p select=?: 
echo.

echo !choice[%select%]! > my-choice.txt

echo  TYPE ANY KEY TO GO BACK IN START MENU ...
pause  >NUL
goto START

Solution

  • You almost have it :)

    echo off
    :START
    cls
    setlocal enabledelayedexpansion
    set count=0
    :: Read in files
    for %%x in (*.*) do (
      set /a count=count+1
      set choice[!count!]=%%x
    )
    echo.
    echo Please choose files by number:
    echo.
    :: Print list of files
    for /l %%x in (1,1,!count!) do (
       echo %%x] !choice[%%x]!
    )
    echo.
    :: Retrieve User input
    set /P selected="select file numbers: "
    for %%i in (%selected%) do (
        echo !choice[%%i]! >> my-choice.txt
    )    
    echo.
    echo  TYPE ANY KEY TO GO BACK IN START MENU ...
    pause  >NUL
    goto START
    

    Please note that this does not check for valid numbers or anything else.