editing multiple text files

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
I have harvested the following code from the link below and would like to enable it to make a given change to all text files in a selected folder.

Advice welcome.

mark :)

http://www.jpsoftwaretech.com/find-and-replace-in-text-files/
Code:
Sub FindAndReplace(filePath As String, findWhat As String, _
                    replaceWith As String)
' from Excel Help:
' A variable-length string can contain up to approximately 2 billion
' (2^31) characters.
Dim nextFileNum As Long
Dim oldFileContents As String
Dim newFileContents As String
Dim textFileTypes() As String
Dim fileExtension As String
If Len(Dir(filePath)) = 0 Then
    Exit Sub
  End If
 
' only act on "text" files
textFileTypes = QuoteString("txt csv html xml", ",")
     fileExtension = LCase$(Right$(filePath, 3))
If UBound(Filter(textFileTypes, fileExtension)) = -1 Then
Exit Sub
End If
' open file and read contents
nextFileNum = FreeFile
Open filePath For Input As #nextFileNum
   oldFileContents = Input$(LOF(nextFileNum), #nextFileNum)
   Close #nextFileNum
' replace old char with new char
   newFileContents = Replace(oldFileContents, findWhat, replaceWith)
' reopen file and write new contents
nextFileNum = FreeFile
Open filePath For Output As #nextFileNum
Print #nextFileNum, newFileContents
Close #nextFileNum
End Sub
Function QuoteString(str As String, delimiter As String) As String()
 Dim tempString() As String
Dim newString As String
 newString = Replace(str, " ", delimiter)
' split the string into an array, using delimiter
tempString = Split(newString, delimiter)
QuoteString = tempString
End Function
 
Sub TestFindAndReplace()
 
On Error GoTo ErrorHandler
Call FindAndReplace("V:\Mark\VBA_Excel\test data app\Sample Data\ReplaceInTextFile.txt", "UnderwriterName=IPA", "coolio")
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Also, I was wondering how to include the 'like' function in this part of the code:

Code:
Sub TestFindAndReplace()
 
On Error GoTo ErrorHandler
Call FindAndReplace("V:\Mark\VBA_Excel\test data app\Sample Data\ReplaceInTextFile.txt", [B]like "UnderwriterName=*",[/B] "coolio")
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,613
Messages
6,186,005
Members
453,334
Latest member
Prakash Jha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top