Lookup and append ini file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
FiZz_
Posts: 3
Joined: 16 Sep 2013 01:15

Lookup and append ini file

#1 Post by FiZz_ » 16 Sep 2013 01:24

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Lookup and append ini file

#2 Post by foxidrive » 16 Sep 2013 04:21

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.

FiZz_
Posts: 3
Joined: 16 Sep 2013 01:15

Re: Lookup and append ini file

#3 Post by FiZz_ » 16 Sep 2013 07:57

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Lookup and append ini file

#4 Post by foxidrive » 16 Sep 2013 19:07

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

FiZz_
Posts: 3
Joined: 16 Sep 2013 01:15

Re: Lookup and append ini file

#5 Post by FiZz_ » 17 Sep 2013 00:11

Perfection.

Thank You for your assistance.

Post Reply