Solved Read lines from .txt file in DOS?

Compaq HP
April 9, 2007 at 17:46:34
Specs: WinXP Pro, 3.0GHz Pentium 4, 1GB RAM
I'm trying to read single lines from a text file and store each line in a variable so that I can use that variable to do something (such as COPY, RENAME, MOVE, etc). Here is the operation I want:

1) Read 1st line from txt
1) Store that line in a variable so that I can do something with it (e.g. COPY)
2) Read 2nd line from txt
2) Store that line in the same variable (overwriting the previous info)
2) Run command using that var
3) Read 3rd line from txt
3) Store that line in the variable
...and so on.

Is this possible in Windows XP DOS? If so, how can it be done? My guess is a FOR loop is involved but I'm not sure after that.

(I posted this in WinXP forum but I don't know why they deleted it, maybe this is the right place)



See More: Read lines from .txt file in DOS?

Report •

✔ Best Answer
April 23, 2007 at 12:18:09
::==
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
if !myvar!'==' set myvar=%%a
)
echo myvar is set to !myvar!
::==


=====================================
If at first you don't succeed, you're about average.

M2




#1
April 9, 2007 at 21:26:00
::==
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
echo do my commands on %%a
)
::==



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#2
April 10, 2007 at 10:41:00
I think that just outputs the entire text file to the screen at once. I'm trying to do it line-by-line. Doesn't look like there's a way to stop it after each line.

Report •

#3
April 10, 2007 at 14:49:12
As per Mechanix script with a pause. That will cause a stop after every line

for /f "tokens=* delims= " %%a in (myfile) do (
echo do my commands on %%a
pause
)


Report •

Related Solutions

#4
April 10, 2007 at 21:21:43
What I want to do is to be able to store every line in a variable and USE that variable after every line. I'm not trying to read the file, I know what's in it. I want to use the data from the file (in a variable) to perform file management operations.

Report •

#5
April 10, 2007 at 22:03:31
::==
:: gets lines into vars c1 v2 v...

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set /a n+=1
set v!n!=%%a
)
set v
::==



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#6
April 16, 2007 at 10:05:09
I'm looking for something very similar to this, but insteady, I only need one line to set one system variable.

-locate first line in Temp.txt
-Set %CLIENTNAME% per that line in Temp.txt

Tried some mods to the code above, but couldn't get it.

Your response is appreciated!

PM


Report •

#7
April 23, 2007 at 12:18:09
✔ Best Answer
::==
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
if !myvar!'==' set myvar=%%a
)
echo myvar is set to !myvar!
::==


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#8
July 31, 2013 at 07:15:25
Is this thread still viable?

6+ years

SHEESH

=====================
M2 Golden-Triangle


Report •

#9
July 31, 2013 at 08:59:57
Well, it's still the best answer?

How To Ask Questions The Smart Way


Report •

Ask Question