1

I'm working on a batch file that generates a text file with a list of names in the format of

Name1
Name2
Name3
etc...

What I would like to do is create a menu that pulls the entries from the text file to create the options.

So

Menu
1 Name1
2 Name2
etc...

I'm ok with creating the menu itself but it's getting the data from the file and assigned to variables that is stumping me. I've looked at the FOR command but I guess my brain is just not wrapping around it. Does anyone have any code that does the above?

Any assistance would be greatly appreciated.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Sorry, didn't pay attention to the preview pane. The text file entries are single per line. Some of them will have spaces in them for example "Microsoft Office Document Image Writer" would be an entry. – user1418000 May 25 '12 at 18:34

2 Answers2

1
@echo off
setlocal EnableDelayedExpansion
echo Menu
set i=0
for /F "delims=" %%a in (theTextFile.txt) do (
   set /A i+=1
   set "name[!i!]=%%a"
   echo !i! %%a
)
set lastOpt=%i%
:getOption
set /P "opt=Enter desired option: "
if %opt% gtr %lastOpt% (
   echo Invalid option
   goto getOption
)
echo Process !name[%opt%]!

For a more detailed explanation, review this answer

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

If you are generating the text file line by line in a loop, you could add the following counter to the loop:

…
SET /A cnt+=1
…

Then you would use it in another loop, the one that would output the lines in the form of a numbered list:

…
<YourFile.txt (
  FOR /L %%L IN (1,1,%cnt%) DO (
    SET /P line=
    SETLOCAL EnableDelayedExpansion
    ECHO %%L !line!
    ENDLOCAL
  )
)

If, however, your text file is generated as an output of a single command (or maybe several commands), you could count the lines afterwards like this:

…
FOR /F %%C IN ('FIND /C /V "" ^<YourFile.txt') DO SET cnt=%%C
…

Then you can use the same FOR /L loop as above to display the menu.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • Yes but, how an option of this menu would be choosen and processed? – Aacini May 27 '12 at 16:50
  • It seems like I overlooked the *assigned to variables* part of the question. Not sure I understand it, though, but I hope you do. :) – Andriy M May 27 '12 at 18:27