Computing.Net > Forums > Disk Operating System > Read a configuration file into a batch file

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 a configuration file into a batch file

Reply to Message Icon

Name: fmaloto
Date: June 5, 2009 at 07:53:42 Pacific
OS: Windows XP
Subcategory: General
Comment:

Hello All --

I'm somewhat a novice when working with the DOS command-line and I wanted to reach out to see if I could get an answer to my problem or be pointed in the right direction.

Here goes:
I originally created a batch file that performed a routine and wrote out a results.txt file.

I later then updated the batch file so that I could dynamically name the results.txt file based on the date/time (results_20090605.txt).

Now, I'd like to further enhance my batch file to read in a configuration file (another text file) which would contain specific information on separate lines.

How do I read each line of the configuration file into its own variable in the batch file?

What happens if I have comments above each value? How do I ignore those?

example within configuration file:
:: Site URL
http://www.google.com

:: Browser
IE

Any and all help would be greatly appreciated.

And thanks much in advance.
FM



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 6, 2009 at 01:31:09 Pacific
Reply:

If we assume you're using XP, which is NT; not DOS, because results_20090605.txt is not a DOS filename:
=================================
@echo off & setLocal EnableDelayedExpansion

set N=0

for /f "tokens=* delims= " %%a in ('find /v "::" ^< myconfig') do (
set /a N+=1
set v!N!=%%a
)


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

M2


0

Response Number 2
Name: fmaloto
Date: June 10, 2009 at 12:46:24 Pacific
Reply:

Thanks M2, but I'm still a bit lost here. And yes, I am using XP. Didn't understand the categorization before I posted the message :(

As I read each line after the comments within the config.txt file, how to assign that value to its own variable in the calling batch file.

In the case above, how would I set the URL to a variable var1 and then set the browser to another variable var2 and so on? From what I see v!N! is replaced when it goes through the next iteration of the loop.

Again, thanks much in advance.
FM


0

Response Number 3
Name: Mechanix2Go
Date: June 10, 2009 at 18:43:48 Pacific
Reply:

You're right that the bat assigns sequential vars. If you need to get more specific, post a fair chunk of the file and explain waht's needed.


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

M2


0

Response Number 4
Name: fmaloto
Date: June 16, 2009 at 10:30:00 Pacific
Reply:

Thanks again M2...!

I figured out my problem by creating a separate FOR loop for each value I wanted to extract.

e.g.,
From config.txt.....

URL=http://employersportalqa.wtc.fyiblue.com
browser=IE7

From batch file.....

FOR /F "tokens=2 delims==" %%a IN ('find "URL" ^<config.txt') DO SET siteURL=%%a

FOR /F "tokens=2 delims==" %%b IN ('find "browser" ^<config.txt') DO SET browserType=%%b


However, I came across a separate issue related to delims.
Question -- How do you handle a string that contains the delims value within that string?

e.g.,
setting=-Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=8080

Here I'm trying to get the following "-Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=8080", but instead I only get "-Dhttp.proxyHost".

Once again, thanks much in advance.
FM


0

Response Number 5
Name: Mechanix2Go
Date: June 16, 2009 at 18:19:50 Pacific
Reply:

@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=1* delims==" %%a in (myfile) do (
echo %%b
)


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

M2


0

Related Posts

See More



Response Number 6
Name: fmaloto
Date: June 16, 2009 at 19:06:36 Pacific
Reply:

M2, It worked beautifully...!

One last question if you would be so kind....

There are instances where my configuration file may not have a value. When I SET the variable and ECHO the variable, I get the following "Echo is off."

Is it possible to set the value to NULL if there is no value to grab from the configuration file?

Truly appreciate the help you're providing.

FM


0

Response Number 7
Name: Mechanix2Go
Date: June 16, 2009 at 19:10:43 Pacific
Reply:

The usual way to avoid that is:

echo.%%i

If that doesn't help post your code with a chunk of the file.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Read a configuration file into a batch file

Can a batch file get a files' date/time stamp? www.computing.net/answers/dos/can-a-batch-file-get-a-files-datetime-stamp/318.html

A batch to read from a file? www.computing.net/answers/dos/a-batch-to-read-from-a-file/4566.html

email with a batch file using blat www.computing.net/answers/dos/email-with-a-batch-file-using-blat/14340.html