Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi there,
i have small script that reads lines of a txt file:
@echo off
echo start
for /f "tokens=1,2* delims= " %%a in (text.txt) do (
echo 1ST--%%a
echo 2ND--%%b
echo 3RD--%%c
set var=%%b
echo new var=%var%
)how do i save those %%a,%%b,.. into new vars? the above code returns me the first word in the last line of the text file.. please help. thanks in advance!

"above code returns me the first word in the last line"
Cal me crazy, but it returns the SECOND word in the last line.
=====================================
If at first you don't succeed, you're about average.M2

lol, try this. use exclamation mark inside a for loop rather than percentage.
set var=%%b
echo new var=!var!add this line in the top of code:
SetLocal EnableDelayedExpansionyes, the above code does return the SECOND word of LAST Line after the loop.

i have another variant of question to this post...
If each line in text.txt is long, passing this line into command is not pretty.
So, I'm thinking of passing each line into a variable, then use this variable in a command line. I can use this variable into filename as well.
if i modified the code to this:
@echo off
SetLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (text.txt) do (
set var=%%a
echo !var!
)but, how do i make each var different for each line parsed from text.txt?
e.g. var1=[line1], var2=[line2], var3=[line3]
assumption is that the qty of lines in text.txt changes, that's why i put token=*.

::filenum.bat
@echo off
SetLocal EnableDelayedExpansionfind /v /n "" text.txt > ~tmp
set /a count=0::store line to array of variables
for /f "skip=2 tokens=1* delims=[]" %%a in (~tmp) do (set var%%a=%%b & set/a count+=1)::display the variable to screen
for /l %%i in (1,1,!count!) do echo var%%i=!var%%i!
del ~tmp > nul

More simply:
========================================
@echo off & setLocal EnableDelayedExpansionset /a N=0
for /f "tokens=* delims= " %%a in (myfile) do (
set v!N!=%%a
set /a N+=1
)set v
=====================================
If at first you don't succeed, you're about average.M2

that's "set v" is a nice trick. everyday i read this forum, i am learning new stuff. i am glad i found this forum.
the difference with using find and direct parsing is in "empty line".

Hi reno,
I learn something almost every day. You would do well to study the code that IVO writes.
Happy New Year gang!
=====================================
If at first you don't succeed, you're about average.M2

thanks M2,
ur script is quite simple and elegant ;)
a good complement to reno's as well.
as reno mentioned, it doesn't parse the myfile nicely if there's empty line anywhere.
thanks guys! and happy new year to all!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |