Home > Software design >  Batch list folder contents, then echo results as options to be set as variables
Batch list folder contents, then echo results as options to be set as variables

Time:04-21

I usually like to try to work these out myself, but at the moment I have to admit I don't know where to start with this one. Hoping someone could kindly steer me in the right direction at least.

I have a folder with a number of .txt files

Text1.txt
Text2.txt
Text3.txt

In my windows bat file I need to list the contents of said folder and set them as options to be set as variables. example:

cls
echo[
echo[                  Please select an option
echo[        
echo                          (1) Text1
echo                          (2) Text2
echo                          (3) Text3
echo[
set /p option=Type your selection (1-3) and press ENTER=

if !option!==1 set var=Text1
if !option!==2 set var=Text2
if !option!==3 set var=Text3

Any advice is greatly appreciated, this forum has been great.

*Edit here is something I tried

cls
echo[
echo[                  Please select an option
echo[        
dir /b "*.txt"
echo[
set /p option=Type your selection (1-3) and press ENTER=

if !option!==1 set var=text1
if !option!==2 set var=text2
if !option!==3 set var=text3

it works, but does not add the numbers (1) before the options, and it also has them aligned left not centred.

                  Please select an option

text1.txt
text2.txt
text3.txt

Type your selection (1-3) and press ENTER=

CodePudding user response:

Well, if this were any other language what would you do? Probably populate an array with your directory listing, right? Then use that array to pair menu option with file choice? Well, don't let the fact that the Batch language doesn't have arrays stop you. They're easy enough to simulate.

@echo off & setlocal

set file.length=0
for %%I in (*.txt) do (
    setlocal enabledelayedexpansion
    for /f %%# in ("!file.length!") do endlocal & set "file[%%~#]=%%~I"
    set /a file.ubound = file.length, file.length  = 1 
)

set file

There you go. The last line should show you a list of all the variables beginning with file, including the simulated .length and .ubound properties. From there, just use a for /L %%I in (0, 1, %file.ubound%) to display your menu. Hopefully this will get you started.

CodePudding user response:

Thanks for your help, trying to combine the two answers so far and this is where I am at

@echo off & setlocal

cls
mode con: cols=95 lines=20
set file.length=0
for %%I in (*.txt) do (
  setlocal enabledelayedexpansion
  for /f %%# in ("!file.length!") do endlocal & set "file[%%~#]=%%~nI"
   set /a file.ubound = file.length, file.length  = 1 
)
set file
echo[
echo[                  Please select an option
echo[        
for /L %%I in (0, 1, %file.ubound%) do (echo %%I
)
echo[
set /p option=Type your selection and press ENTER=
set "var=file[!option!]"
echo !var!
endlocal

I am clearly doing something wrong still (as you will likely see instantly) as the result is not as desired

file.length=3
file.ubound=2
file[0]=text1
file[1]=text2
file[2]=text3

                  Please select an option

0
1
2

Type your selection and press ENTER=0
!var!
  • Related