Save lines of text in variable

Microsoft Windows 7 ultimate 32-bit
May 18, 2010 at 19:47:45
Specs: Windows 7
Hey guys,

I am trying to pull values from a text file and save them to variables in a batch script. Also, I want the script to print out all of the variables when I call them. The amount of lines in the text file will vary based on the user, so that will have to be taken into account when echoing them, right?

Here is an example text file that I would like to read the values from

(-----start of test.txt---------)
BDS-20100516-1646

Fresh1.1

BDS-20100516-1632

BDS-20100211-1346

Fresh2.1.1

(and so on, depending on the user)
(-----end of test.txt---------)

I would like it to save all of those in variables, and then echo them to the console as

1. BDS-20100516-1646

2. Fresh1.1

(and so on, depending on how many there are)

------------------------------------------------------

I need the values stored in variables because I want to call those values later after a:

set /p restore=(one of the options)

then do something with which ever one they choose with an

if %restore% == 1 (do something with the variable)


can you guys help me out? I hope I made this clear.. . I know it probably looks confusing.

I'll try to clean it up if I think of a way :P


See More: Save lines of text in variable

Report •


#1
May 18, 2010 at 21:24:47
here's a simple menu system using "find" instead of storing all values into array (which is also reasonable approach, but,
for me, boring). I think you;ll get the idea how to elaborate further from this sample:
@echo off & setlocal
:: display menu
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" %user%') do (
echo %%a. %%b
)
set /p sel=select from list:
set /a sel-=1
if %sel% equ 0 (set skip="tokens=*") else (set skip="skip=%sel% tokens=*")
:: if you want your index to line up, add here: set /a sel+=1
call :xx %skip% %sel%
goto :eof
:xx
:: now extract index and string-value from the file (alternative to
:: storing variables into array)
:: no "IF" construct required, since index goes to appropriate label.
for /f %1 %%z in (%user%) do echo %2 %%z & call :%2 %%z & goto :eof
:0
echo ZERO: menu item %1 process
goto :eof
:1
echo ONE: menu item %1 processed
goto :eof
:2
echo TWO: menu item %1 being processed
goto :eof

Report •

#2
May 19, 2010 at 01:32:25
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (BDSfile) do (
set /a N+=1 & set V!N!=%%a
)

for /L %%i in (1 1 !N!) do echo %%i. !V%%i!


=====================================
Helping others achieve escape felicity

M2


Report •

#3
May 20, 2010 at 16:08:35
Wow, Mechanix2Go, That worked great

Exactly what I was looking for :)

Now, I wanted them to be able to choose one of them with a set /p like,

Which one?

then they enter a #.

And then it uses that value from the text file to do what I want.

(I'm using it to restore a Nandroid image backup to an Android phone via command line)

how would I go about grabbing one of those saved variables?

say, they choose #4,

how would I call the variable of the 4th item in the list?


Report •

Related Solutions

#4
May 21, 2010 at 00:30:28
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (BDSfile) do (
set /a N+=1 & set V!N!=%%a
)

for /L %%i in (1 1 !N!) do echo %%i. !V%%i!

set /p c=pick one
echo.!V%c%!


=====================================
Helping others achieve escape felicity

M2


Report •

#5
September 10, 2010 at 23:09:25
I forgot to thank you back in May, but thank you very much for the help!

Report •

#6
September 11, 2010 at 03:00:05
Great to hear from you.


=====================================
Helping others achieve escape felicity

M2


Report •

#7
September 11, 2010 at 03:04:45
I really mean it, showing me that opened up a whole new way to code for me, w/ setLocal EnableDELAYedeXpansion, never had used it before, but the possibilities are endless now! (well, almost haha)

Report •


Ask Question