Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically search and replace text within file 1

Status
Not open for further replies.

rockfish12

Technical User
Feb 12, 2002
31
US
I have a situation where I have a bunch of text files - all within the same directory - that need to have a particular line replaced. What I want to do is programmatically open, say n07w113a4.txt, and replace the text on a line in that file with the current filename, minus the extension.
So, for example, if the sixth line was the following:

Tile Identifier #: i307113a4.tif

I would want to replace it with:

Tile Identifier #: n07w113a4.txt

Ideally, I'd like to have the code loop through every text file in the directory, and do the same exercise with each one: that is, replace part of the "Tile Identifier #" line with the file name. The tricky aspect is that the part I want to replace won't be named the same thing every time. It will, however, be on the same line as the "Tile Identifier #:" string. But, the "Tile Identifier:" string won't be in a fixed place every time within the document. It can vary, from being on the sixth line, to the fourth, etc...

Below is the typical layout of the text files:

xxxxxxx
xxxxxxx
xxxxxxx

File Creation date: March 10, 2000
Tile Identifier #: i307113a4.tif
Project Area: West and East Java

xxxxxxx
xxxxxxx
xxxxxxx


If anyone can give me any ideas or sample code on how to do this, I'd greatly appreciate it.


 
I have selected to use Microsoft Word for this task. Insert these modules into Word Visual Basic and hey presto you should have an automated update.

**********************************************

Sub Update_Files()

'set file name search
SearchPath = "C:\TEMP\*.*"

SearchName = Dir(SearchPath)
Do While SearchName <> &quot;&quot;
'select only filenames with .txt extensions
If SearchName <> &quot;.&quot; And SearchName <> &quot;..&quot; And Right(SearchName, 4) = &quot;.txt&quot; Then
'open found file and run routine
Documents.Open FileName:=SearchName
Find_and_Replace
End If
SearchName = Dir 'get next entry
Loop

End Sub

**********************************************

Sub Find_and_Replace()

'find text in document regardless of line number
With Selection.Find
.ClearFormatting
.Text = &quot;Tile Identifier&quot;
.Replacement.Text = &quot;&quot;
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute

'select the complete line
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

'insert new text with document name
Selection.TypeText Text:=&quot;Tile Identifier #: &quot; & ActiveDocument.Name

'save changes and exit current document
ActiveDocument.Save
ActiveWindow.Close

End Sub

**********************************************

I quickly tested this at it worked perfect for me.

Enjoy.
 
Thanks, this looks great. I will give it a try!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top