Hi googooly-ga,
Sorry for the delay in posting your answer, but a problem at my
ISP's end had kept me off the net for the last couple of days. Anyway,
here I am with the answer. :-)
You can download the code from the following URL:
http://member.isavvix.com/theta_ga/Search&Replace.jar
Download the file 'Search&Replace.jar' and rename it
'Search&Replace.ZIP'. You can then easily extract its contents.
The ZIP file contains the following main files:
- GA_Q.vbp: The main VB project file. Click on it to start up VB.
- frmMain.frm: The form which takes in input from the user.
- SearchAndReplace.bas : Implements the function which searches and
replaces text in the given input file.
- Enhancement1.bas: Contains code to implement the functionality
required by the first enhancement as per your question.
- Enhancement2.bas: Contains code to implement the functionality
required by the second enhancement, i.e., reading INI files.
==========================================
When you run the program, the Main form will appear.
At the bottom of this form, there are radio buttons to select the
program mode. The 3 program modes are:
1. Default: In this mode, you can only enter one term to search for.
The program searches for the text specified in the Search Term
textbox, and replaces it with the text specified in the Replace Terms
textbox.
2. Enhancement1: In this mode, the program can search for and
replace multiple search terms. You can specify multiple search terms
in the Search Terms textbox as follows:
"<date>","<title>","<name>"
The Replace Terms are specified as:
"1/1/2003","Manager","Joe User"
Every term should be in double-quotes (") and separated by commas
(,).
3. Enhancement2: In this mode, the search and replace terms are read
in from an ini file. The Search and Replace terms textboxes are not
visible. Instead, you are provided a textbox in which you must enter
the full path and name of the ini file to be used. The ini file should
have the following format:
[Input]
searchterm1=replaceterm1
searchterm2=replaceterm2
searchterm3=replaceterm3
...
The zip file you downloaded will contain a sample ini file named
config.ini
The form will also have two textboxes at the very top. The first
textbox allows you to enter the full path and name of the input text
file. A sample input file 'test.txt' is contained in the zip file you
downloaded.
The second textbox is where you will provide the name of the new file
in which the results of the search and replace operation are to be
saved. You need to provide only the name, not the full path. This file
will be created in the same folder as the input file.
Once you have filled the required data in the textboxes, click on the
Start button. A new file will be created in which the results of the
operation are stored.
=============================================
This program is of the level of an intermediate VB programmer. While
you are reading the code, be sure to keep a copy of the MSDN library
handy, or check out its online version at http://msdn.microsoft.com
Although I have commented the code pretty liberally, you might have
some problems if you are not familiar with the following topics:
- The VB String functions: Len, Mid, Left, Right, InStr
- Dynamic Arrays In VB
- The FileSystemObject Object
- Using the Win32 API functions in VB
- INI files/ GetPrivateProfileSection() WIN32 function
If you are not very comfortable with the above topics, you might want
to check out the following articles:
- The VB String functions
- Juicy Studio Visual Basic String Functions
( http://www.juicystudio.com/tutorial/vb/strings.html )
- Visual Basic Parsing Strings
( http://www.rentron.com/parsingstrings.htm )
- Dynamic Arrays In VB
- Developer Fusion: Arrays - Dynamic Arrays
By James Crowley
( http://www.developerfusion.com/show/19/5/ )
- The FileSystemObject Object
- MSDN Library: FileSystemObject Object
( http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vaobjFileSystemObject.htm)
- The FileSystemObject and associates
( http://www.vbwm.com/art_1999/whatsnew/filesys.asp )
- Using the Win32 API functions in VB
- Using Win32 API - Online tutorial
( http://www.webwareindex.com/tutorials/API.html )
- Using the Win32 API in VB
by Sreejath
( http://www.student.kuleuven.ac.be/~m0116986/agnet/tutors/iapi2p.htm
)
- INI files/ GetPrivateProfileSection() WIN32 function
- Visual Basic : Mastering INI files
( http://www.vbexplorer.com/VBExplorer/focus/ini_tutorial.asp )
- MSDN Library: GetPrivateProfileSection
( http://msdn.microsoft.com/library/en-us/sysinfo/base/getprivateprofilesection.asp
)
=============================================
Here is a synopsis of how the program works. The search/replace terms
that you provide as input are stored in two separate arrays. If you
are working in the default program mode, there will only be 1 search
term and 1 replace term, so the arrays will contain only one element
each.
If you are working in Enhancement1 mode, you provide an input
string, which can contain any number of search/replace terms. These
input strings are passed on to a function which parses these strings,
extracts the search and replace terms one by one, and adds them to the
arrays, dynamically resizing the arrays so they have enough space to
hold the new elements.
If you are working in Enhancement2 program mode, the search terms
are read in from the ini file you specified. Here also, a function
reads in the complete text from the ini file, extracts the search and
replace pairs from it one by one, and adds them to the respective
array. The function uses the GetPrivateProfileSection() WIN32 API
function to read data from the INI file. For more information on this
function, check out the article I have linked to in the previous
section.
Once the Search and Replace arrays have been filled with the search
and replace terms you provided, they are passed to the
SearchAndReplace function. This function reads in the text from the
input file into a string, searches and replaces the text in this
string, and then writes the result into the new file that it creates.
============================================
Hope this helps.
If you need any clarifications, just ask!
Happy Coding!
Regards,
Theta-ga
:-)
============================================
Google Search Terms Used:
visual basic string mid len left right instr
filesystem object visual basic
using win32 api in vb |