Computing.Net > Forums > Programming > Read lines from .txt file in DOS?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read lines from .txt file in DOS?

Reply to Message Icon

Name: GuntherSaxony
Date: April 9, 2007 at 17:46:34 Pacific
OS: WinXP Pro
CPU/Ram: 3.0GHz Pentium 4, 1GB RAM
Product: Compaq HP
Comment:

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)




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 9, 2007 at 21:26:00 Pacific
Reply:

::==
@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



0

Response Number 2
Name: GuntherSaxony
Date: April 10, 2007 at 10:41:00 Pacific
Reply:

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.


0

Response Number 3
Name: cup
Date: April 10, 2007 at 14:49:12 Pacific
Reply:

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
)


0

Response Number 4
Name: GuntherSaxony
Date: April 10, 2007 at 21:21:43 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: April 10, 2007 at 22:03:31 Pacific
Reply:

::==
:: 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



0

Related Posts

See More



Response Number 6
Name: thegoat00
Date: April 16, 2007 at 10:05:09 Pacific
Reply:

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


0

Response Number 7
Name: Mechanix2Go
Date: April 23, 2007 at 12:18:09 Pacific
Reply:

::==
@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



0

Sponsored Link
Ads by Google
Reply to Message Icon

line pattern perl program... vb.net DataGridView quest...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read lines from .txt file in DOS?

reading lines from text file (java) www.computing.net/answers/programming/reading-lines-from-text-file-java/12276.html

Read last line from newest file in folder www.computing.net/answers/programming/read-last-line-from-newest-file-in-folder/19280.html

batch file to read all lines from txt file www.computing.net/answers/programming/batch-file-to-read-all-lines-from-txt-file-/19834.html