Page 1 of 1

Lookup and append ini file

Posted: 16 Sep 2013 01:24
by FiZz_
Hi All.

Thanking you in advance:

I am looking for a way to resolve a hostname and then append an ini file with the ip address. The problem that I am experiencing is that in the ini file, there are multiple Ip addresses and I only want to update the IPAddress line under one of the fifty headings. Here is an example of the ini file which contains 50 exact listings like below with the only point of reference being the [Headind] on top of each section.

[Estacion 1]
Version=1
Channel=SOCKET
Cpu=0
Station=1
AutoStation=0
Retry=3
DontClose=0
PguMode=0
PhoneNumber=
CountryCode=0
AreaCode=0
Location=
UseDialing=0
DeviceName=
IPAddress=189.14.80.10
FdlStation=0

Cheers
FiZz

Re: Lookup and append ini file

Posted: 16 Sep 2013 04:21
by foxidrive
Are there any blank lines in the file that you need to retain?

You can read the INI file and write to a temp file - comparing each line for the heading that you need to change.

When the heading is located, set an environment variable so that the next time the
IPAddress=
is located then it will modify the IP address - and clear the variable so it doesn't change any further lines.

Re: Lookup and append ini file

Posted: 16 Sep 2013 07:57
by FiZz_
Thanks for the speedy reply

There are not blank lines whatsoever.

Please could you give me an example of how to compare and set the environment variable.

Re: Lookup and append ini file

Posted: 16 Sep 2013 19:07
by foxidrive
This does the job here:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set ip=111.222.333.444
set "found="
del file.new 2>nul
for /f "delims=" %%a in (file.ini) do (
 set "line=%%a"
   if "%%a"=="[Estacion 1]" set found=1
   if not defined found (>>file.new echo %%a)
     if defined found (
        if "!line:~0,10!"=="IPAddress=" (>>file.new echo IPAddress=%ip%&set "found=") else (>>file.new echo %%a)
     )

)
pause

Re: Lookup and append ini file

Posted: 17 Sep 2013 00:11
by FiZz_
Perfection.

Thank You for your assistance.