Sign in to follow this  
Followers 0
dbs179

Changing a few lines of text in a custom .config file?

10 posts in this topic

Hey all,

I have a small little project I am working on the help re-point some client workstation to new servers (SQL) that I am installing. I have most of the code figured out and it works like a charm. Besides adding/configuring new System DSN' in the ODBC, I have to edit two different files. One is a .ini file and the other is a .config file. I got the ODBC and the .ini file all sorted out, but I am not sure where to start on the .config file. Here are the first few lines of the file that I want to edit.

<?xml version="1.0" encoding="UTF-16"?>

<configuration>

<appSettings>

<add key="ApplicationServerUri" value="http://localhost:9111/Lnl/ApplicationServer.rem"></add>

<add key="SkipSchemaGeneration" value="True"></add>

<add key="ConnectionString" value="Data Source=OLDSERVER; Integrated Security=SSPI; Initial Catalog=OLDDATABASE"></add>

<add key="CustomDatabaseType" value=""></add>

<add key="DatabaseType" value="SqlServer"></add>

<add key="Lnl.LicenseSystem.Client.Host" value="OLDSERVER"></add>

I want to change the "OLDSERVER" in line 6 the "OLDDATABASE" in line 6 and the "OLDSERVER" in line 9 to a set value for example "NEWSERVER" and "NEWDATABASE" regardless of the text that is already in those entries. I was thinking that I would need to use StringInStr, but I'm not sure. I've only written a few small things in AutoIT and most of it I was able to find either easily on here, or in the help file. Since this file is a different extension than the .ini I am kind of lost. Any help would be awesome.

Thanks Dave.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello dbs179,

Use _ReplaceStringInFile if you want to change directly in your file,

or use _ArrayFindAll and StringReplace if you prefer to make an array.

Edited by Jikoo

My projects : GCS Search 1.07 (GUI Control Styles Search)* Multilingual tool to help about the old scripts, with a dynamic menu.* Easy way to find missing Include files in old scripts downloaded from forums !* Famous Monoceres script added and improved (visual, drag and drop, automatic)* There is an interactive color converter with palettes too, for fun !The best way to start Autoit3 development (for newbies).

Share this post


Link to post
Share on other sites

Hello dbs179,

Use _ReplaceStringInFile if you want to change directly in your file,

or use _ArrayFindAll and StringReplace if you prefer to make an array.

Cool, thanks for pointing me in the right direction. I'm looking those up right now.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

To help to you, see The best way to start Autoit3 development

Edited by Jikoo

My projects : GCS Search 1.07 (GUI Control Styles Search)* Multilingual tool to help about the old scripts, with a dynamic menu.* Easy way to find missing Include files in old scripts downloaded from forums !* Famous Monoceres script added and improved (visual, drag and drop, automatic)* There is an interactive color converter with palettes too, for fun !The best way to start Autoit3 development (for newbies).

Share this post


Link to post
Share on other sites

Hello dbs179,

Use _ReplaceStringInFile if you want to change directly in your file,

or use _ArrayFindAll and StringReplace if you prefer to make an array.

Jikoo,

I found that in the help file, but it looks like it looks for a defined value. In some instances the text I am looking to replace may be different. Is there anyway to do this much like the iniwrite to always replace text in specific position regardless of what is currently there?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You want to change the INI file and the XML file, isn't it ?

So, use my first post to change in XML ; and use Ini functions from the help (in File, Directory and Disk from the AutoIt help file) to change in INI. Sorry, I never works with INI files ! :mellow:

Edited by Jikoo

My projects : GCS Search 1.07 (GUI Control Styles Search)* Multilingual tool to help about the old scripts, with a dynamic menu.* Easy way to find missing Include files in old scripts downloaded from forums !* Famous Monoceres script added and improved (visual, drag and drop, automatic)* There is an interactive color converter with palettes too, for fun !The best way to start Autoit3 development (for newbies).

Share this post


Link to post
Share on other sites

You want to change the INI file and the XML file, isn't it ?

So, use my first post to change in XML ; and use Ini functions from the help (in File, Directory and Disk from the AutoIt help file) to change in INI. Sorry, I never works with INI files ! :mellow:

Jikoo, sorry to keep bothering you. I have the .ini file all figured out. My question is, in the help file for _ReplaceStringInFile, I'm not sure that all of the data I will be looking for will be the same. In some instances, it can be an ODBC connection that I did not set up and could be named a number of things. I have no way of finding out what each ODBC name would be with out logging on to each machine and looking. With iniwrite I can replace a specific line in an ini file with text that I specify, regardless of text is already on that line. I am hoping to do the same thing here. I want to replace the data on both line 6 and 9. Maybe _FileWriteToLine is what I need?

Share this post


Link to post
Share on other sites

_FileWriteToLine is working. Thanks for helping me get started.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hey all,

I have a small little project I am working on the help re-point some client workstation to new servers (SQL) that I am installing. I have most of the code figured out and it works like a charm. Besides adding/configuring new System DSN' in the ODBC, I have to edit two different files. One is a .ini file and the other is a .config file. I got the ODBC and the .ini file all sorted out, but I am not sure where to start on the .config file. Here are the first few lines of the file that I want to edit.

<?xml version="1.0" encoding="UTF-16"?>

<configuration>

<appSettings>

<add key="ApplicationServerUri" value="http://localhost:9111/Lnl/ApplicationServer.rem"></add>

<add key="SkipSchemaGeneration" value="True"></add>

<add key="ConnectionString" value="Data Source=OLDSERVER; Integrated Security=SSPI; Initial Catalog=OLDDATABASE"></add>

<add key="CustomDatabaseType" value=""></add>

<add key="DatabaseType" value="SqlServer"></add>

<add key="Lnl.LicenseSystem.Client.Host" value="OLDSERVER"></add>

I want to change the "OLDSERVER" in line 6 the "OLDDATABASE" in line 6 and the "OLDSERVER" in line 9 to a set value for example "NEWSERVER" and "NEWDATABASE" regardless of the text that is already in those entries. I was thinking that I would need to use StringInStr, but I'm not sure. I've only written a few small things in AutoIT and most of it I was able to find either easily on here, or in the help file. Since this file is a different extension than the .ini I am kind of lost. Any help would be awesome.

Thanks Dave.

Welcome to the world of Regular Expressions!

$sConnectionRegExp = '(<add key="ConnectionString" value="Data Source=)(?U:.*)(;.*Initial Catalog=)(?U:.*)("></add>)'
$sLicenseRegExp = '(<add key="Lnl.LicenseSystem.Client.Host" value=")(?:.+)("></add>)'


$sConfigFile = FileRead("Config.xml")

$sConfigFile = StringRegExpReplace($sConfigFile,$sConnectionRegExp,"\1NEWSERVER\2NEWDATABASE\3")
$sConfigFile = StringRegExpReplace($sConfigFile,$sLicenseRegExp,"\1NEWSERVER\2")

$fhNewConfig = FileOpen("NewConfig.xml",2)
FileWrite($fhNewConfig,$sConfigFile)
FileClose($fhNewConfig)

Of course, once you're happy with the results, you can just as easily overwrite the existing Config.xml instead of writing to a newconfig file (but maybe backup your old one first!).

Edited by ResNullius

Share this post


Link to post
Share on other sites

I use XML DOM functions to do .config changes... you may want to consider it... because XML is volatile and string matches can fail.

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0