Computing.Net > Forums > Programming > Batch File Read Lines from .txt

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.

Batch File Read Lines from .txt

Reply to Message Icon

Name: stick
Date: August 14, 2008 at 06:47:07 Pacific
OS: Windows
CPU/Ram: ?
Product: 2003
Comment:

Hi,

I have a text file that has four lines of text as follows:

Hello
Planet
Welcome
Goodbye

I have a batch file that I would like to read in each line and set each line to a variable. For example my batch file is as follows:

set /p hostname=<variables.txt
set /p backupdrive=<variables.txt
set /p backupdir=<variables.txt
set /p xbackupdir=<variables.txt

This does not work because it is only reading in the first line each time, so each variable is:
hostname = Hello
backupdrive = Hello
backupdir = Hello
xbackupdir = Hello

I would like it to read:
hostname = Hello
backupdrive = Planet
backupdir = Welcome
xbackupdir = Goodbye

I know I can manually change these variables in the batch file, but I have a bunch of other batch files that have the same variables. And I would like to automate this process and only have the user edit one file (variables.txt).

I have searched for hours and days on this topic but I cannot seem to find anything. If anyone can help, it would greatly be appreciated. Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 14, 2008 at 08:15:43 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
set v!N!=%%a
)
set hostname=!v1!
set backupdrive=!v2!
set backupdir=!v3!
set xbackupdir=!v4!


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

M2


0

Response Number 2
Name: stick
Date: August 14, 2008 at 08:42:29 Pacific
Reply:

BRILLIANT!!! Thank you so much!


0

Response Number 3
Name: Judago
Date: August 21, 2008 at 02:40:39 Pacific
Reply:

I know this is already solved but I would like to point out that the method you used in your post almost worked.

Instead of inputting the whole text file into each line, input the whole text file into a number of lines by calling a label (or using a second batch file).


call :label<variables.txt

goto otherstuff

:label
set /p hostname=>nul
set /p backupdrive=>nul
set /p backupdir=>nul
set /p xbackupdir=>nul
goto :eof

:otherstuff


Just don't forget the "goto :eof" line (the label dosen't have to exist). It won't end the batch, it will only end the call.


0

Response Number 4
Name: Mechanix2Go
Date: August 21, 2008 at 20:59:15 Pacific
Reply:

Obviously, I mised the point.


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

M2


0

Response Number 5
Name: Judago
Date: August 22, 2008 at 01:13:11 Pacific
Reply:

I don't think you missed the point.

Your solution has the advantage of being more efficient, especially if there is a large number of variables to be set from the text file. With the other method each variable would need it's own set /p line, even if it's not going to be used, in contrast with just using !Vx!.

Your method can even adapt to files with a different number of lines without modification, whilst the other can't, they either match, get lost or don't get set at all. The later is subjectively the worst because the batch would even stop and wait for input from the user, possibly hundreds or even thousands of times.

I was just trying to say it *could* be done that way.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






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: Batch File Read Lines from .txt

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

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

Batch File: Integers in a Txt File Variable www.computing.net/answers/programming/batch-file-integers-in-a-txt-file-variable/19900.html