1.

Solve : How to read a file with delimitted name value pairs using DOS batch script??

Answer»

0
down vote favorite I have a file with each line of the format name=value. I need to read it using a DOS script and store all the name value pairs in memory. The names are from a predefined list of names, so I can have a list of DOS VARIABLES, and assign values to them as and when a line is read from the file. Please provide the script for doing this. I'm not able to even do as much as read a file using the below code which I got on the internet, it printes nothing:
FOR /F %i IN (regfort.properties) DO @echo %i
Quote from: criminal on February 13, 2011, 07:27:13 AM

0
down vote favorite I have a file with each line of the format name=value. I need to read it using a DOS script and store all the name value pairs in memory. The names are from a predefined list of names, so I can have a list of DOS variables, and assign values to them as and when a line is read from the file. Please provide the script for doing this. I'm not able to even do as much as read a file using the below code which I got on the internet, it printes nothing:
FOR /F %i IN (regfort.properties) DO @echo %i

Please provide a sample of the file you want to read, a few lines will do.
The file TOBE read is like this:
name1=value1
name2=value2

As the names are pre-defined, when the batch file reads the first line it will set the variable %name1% to value1 and while reading the 2nd line it will set the variable %name2%.for /f "delims=" %%i in (regfort.properties) do @set %%iVow, that works. Thanks a ton!
One more small help I need. If the line starts with # I want to OMIT processing it as it is a comment. How to make this additional change?If you only want to exclude lines where # is the first character then this should work...

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in (regfort.properties) do (
set line=%%i
if not "!line:~0,1!"=="#" set %%i
)

...or, if you are content to exclude lines which have a # character anywhere (not just the beginning) you can have it on just one line

for /f "delims=" %%i in (Test.txt) do @echo %%i | find "#">nul || @set %%i

Quote from: Salmon Trout on February 13, 2011, 09:32:28 AM
...or, if you are content to exclude lines which have a # character anywhere (not just the beginning) you can have it on just one line

for /f "delims=" %%i in (Test.txt) do @echo %%i | find "#">nul || @set %%i

why not
Code: [Select]for /f ..... (findstr /v "#" test.txt) do (
.......
)
then you only call findstr (or find) once.That works, thanks.
Now I want to enhance the script so that the file path is not hardcoded in the file:
set RF_PROPERTIES=%HOME%\conf\properties\regfort-monitor.properties

%HOME% points to C:\program files\Hugh systems

When I run
for /f "delims=" %%i in (%RF_PROPERTIES%) do (
set line=%%i
echo line=%line%
if not "!line:~0,1!"=="#" set %%i
)

It complains
The system cannot find the file C:\Program.

Then when I tried "%RF_PROPERTIES%" in the for loop it complains
Environment variable C:\Program Files\Hugh not defined.

How to get it working?Surround the varible in quotesI already tried "%RF_PROPERTIES%" (in quotes) like I already said.
One more thing I just noticed, it still tries to use the lines starting with # and prints
Environment variable # Uncomment and change these properties is not definedSetlocal enabledeyledexpansion
Set a=%RF_PROPERTIES:~0,1%
if %a%==" (set b=%RF_PROPERTIES%) else (set b=""%RF_PROPERTIES"")
for /f "tokens=1,2 delims==" %%a in (%b%) do (
set line=%%a
echo line=!line!
if not "!line:~0,1!"=="#" set %%a=%%b)

note this may not work for two reasons
A) it's late
B) I wrote it on a mobile phone so it's not testedQuote from: mat123 on February 14, 2011, 05:17:52 AM
enabledeyledexpansion

Well, that's not spelled right


Discussion

No Comment Found