Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
IEAny and all help would be greatly appreciated.
And thanks much in advance.
FM

If we assume you're using XP, which is NT; not DOS, because results_20090605.txt is not a DOS filename:
=================================
@echo off & setLocal EnableDelayedExpansionset 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

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

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

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=IE7From 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=8080Here 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

@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

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

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |