1

I am having an issue wrapping my head around this. The thing I am trying to do is list the contents of a text file (%textfile%) with a number in front that increments each time a new line of the text file is printed, then assign that number to the output. It must be in the form of a batch file for my purposes.

Example:

for /f "delims=" %%A in (%textfile%) do (echo %%A & echo.)

Output:

Output1

Output2

Output3

... etc

What I would like it to do:

1. Output1

2. Output2

3. Output3

... etc

This is to be used in a menu that asks you to choose one of the above. Selecting 1 would set Output1 to a variable to be used in another script.

set /P menuSelect=Please make a selection: 
for /f "delims=0-%highestVariable%" %%a in ("%menuSelect%") do echo Incorrect input, press any key to try again & pause>nul & goto :otherFunction

Something else I am trying to figure out is how to set the delimter equal to 0 through the highest numerical output in the above code. That's why I set "delims=0-%highestVariable%".

So essentially, how do I output the contents of the text file with a number assigned, then allow the user to select one of the numbered outputs and assign it to a variable.

Any help is appreciated, been stuck on this for a few days.

2 Answers2

2

simply add a counter (you'll need delayed expansion):

@echo off
setlocal enabledelayedexpansion
set count=0
for /f "delims=" %%A in (%textfile%) do (
  set /a count+=1
  echo !count!. %%A & echo.
)
set numbers=123456789
set numbers=!numbers:~0,%count%!
echo %numbers%
echo on
set /p "select=number: "
for /f "delims=%numbers%" %%a in ("%Select%") do echo Incorrect input, press any key to try again & pause>nul & goto :otherFunction

Please note, your delims trick may not work reliable with counters >=10.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I like this and it solves the problem of displaying them (thanks for the tip on delayed expansion), but it does not set each selection to a variable. How would I make my input of 1 equal whatever %%A is next to 1? By my understanding, it's output like this `1. %%A` `2. %%A` `3. %%A` How would I make it `1. %%A` `2. %%B` `3. %%C` so my input chooses what's next to the number? Sorry if I am not making sense, still learning. – Joshua Carswell Aug 08 '16 at 06:43
  • Nevermind! I solved this by adding: `set txt!numCount!=%%A` in the FOR command. Variables txt1 and txt2 displayed correctly as options 1 and 2. – Joshua Carswell Aug 08 '16 at 07:01
0

another way of counting,

@echo off
set/a numOptions=0
for /F "tokens=1,* delims=[]" %%A in ('"type "%textfile%"|find /N /V """') do (
  echo %%A. %%B
  set/A numOptions+=1 
)
echo you have %numOptions% lines in %textfile%
...
...
...
rem your code here
elzooilogico
  • 1,659
  • 2
  • 17
  • 18