Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Need some code snipets for search/replace functions

Hello all, first time poster!

I am trying to develop a little utility to help some of the folks out at the help desk I work at. The company uses an ancient program that can be setup in a server/client environment...however, there are about 3 different text files (.ini's) that contain references to the servername.

I would like to write a utility that has a user input field and a button, and for the input box we could put in the server name so that when we hit the button it can make all of the changes. (We CONSTANTLY run into problems with this application, when you use the program and its setup options to point to a new server, it almost always leaves traces behind that cause problems.

It's been a VERY long time since I did any programming, and I am currently using microsoft visual basic.net 2005 express edition. Any code snippets you have to share would be most appreciated.

I would mainly need help with a search and replace snippet of code...but I would like for it to be able to search the text file for a line that starts with something like "servername=whatever" and if it exists, totally replace that line with "servername=newserver".

That may be enough to get me started, thanks in advance!
[1267 byte] By [utaeladil] at [2007-11-11 10:07:46]
# 1 Re: Need some code snipets for search/replace functions
I would probably use the standard Windows API functions for updating INI files. See if this helps: http://www.developer.com/net/asp/article.php/3287991
Phil Weber at 2007-11-11 20:48:33 >
# 2 Re: Need some code snipets for search/replace functions
That would be great if we could do that, however the application that uses .ini files is not one we developed, its an older program (guess they still use it because they dumped tons of money into the licensing years ago)...ideally the company SHOULD dump tons more money into the newer version of this application but thats something I have no control over. In the meantime, we are stuck with an app that uses ini files...and we would just like to simplify the troubleshooting as much as we can, since we are stuck with it :(
utaeladil at 2007-11-11 20:49:33 >
# 3 Re: Need some code snipets for search/replace functions
utaeladil,

I'm confused. The URL that Phil supplied has code for working with INI files using VB.Net. Isn't that what you need to do?

Kerry Moorman
kmoorman at 2007-11-11 20:50:37 >
# 4 Re: Need some code snipets for search/replace functions
I will double check it, I only glanced briefly earlier but I thought it was code on how to replace the need for ini's by modifying the main program... not how to edit text inside of a text file... I'll go double check in a few when I get more time...
utaeladil at 2007-11-11 20:51:38 >
# 5 Re: Need some code snipets for search/replace functions
You need to look at the linked page again.

Perhaps you are not understanding what .INI files are. While they are text-based, they also have a specific format that allows you to read them almost like a small database, or, better example, almost like an XML file.

While you could read & write to them using regular textfile processing commands, using the API functions to work with them is a much better approach. You could easily render a .INI file unusable by reading & writing it as a textfile. Using the API functions is much better & safer, not to mention easier; searching a plain textfile for a specific string is much more tedious and slower than indexing into a formatted file.

If you look again at the linked webpage, you will see that it contains a class that allows you to easily use the API functions from VB.NET. Give it a shot. You really don't want to take the chance of scrambling an .INI file that an old, unsupported app depends on. Believe me, I've been there...

-Andrew
Andrew Cushen at 2007-11-11 20:52:36 >