?
Solved

Precision Search and Replace

Posted on 2003-03-02
5
Medium Priority
?
152 Views
Last Modified: 2010-05-01
I need to do the following:

1. Read a file
2. Find the string R85.01.01_04
3. Replace all occurances of R85.01.01_04 with R85.01.00_04 while leaving everything else perfectly intact.

Basically what this needs to do is find the string, change one byte of that string, and leave everything else alone.  The string occurs twice in the file.

Thanks!
0
Comment
Question by:jerw134
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
RanaHossain earned 200 total points
ID: 8051244
a normal replace would do

strFile = replace(strFile, "R85.01.01_04", "R85.01.00_04")

thats it... to read a text file you can use the following... its also use another routine to confirm file is there.

'---------------------------------------------------------------------------------------
' Procedure : readFile
' Author    : rana
' Purpose   : reads and return filetext
'---------------------------------------------------------------------------------------
Public Function readFile(ByVal FilePath As String, ByRef retVal As String) As Boolean
    readFile = FileExists(FilePath)
    If Not readFile Then Exit Function
    Dim iFileHandle As Integer
    Dim strBuffer As String
    Dim lngBufferSize As Long
    iFileHandle = FreeFile
    lngBufferSize = FileLen(FilePath)
    strBuffer = Space(lngBufferSize)
    Open FilePath For Binary Access Read As #iFileHandle
    Get #iFileHandle, 1, strBuffer  'The easy way!
    Close #iFileHandle
    retVal = strBuffer
End Function

'---------------------------------------------------------------------------------------
' Procedure : FileExists
' Author    : rana
' Purpose   : checks if a perticular file exists
'---------------------------------------------------------------------------------------
Public Function FileExists(ByVal FilePath As String) As Boolean
    Dim sFile As String
    On Error Resume Next
    FileExists = False
    sFile = Dir$(FilePath)
    If (Len(sFile) > 0) And (Err = 0) Then
        FileExists = True
    End If
    On Error GoTo 0
End Function
0
 

Author Comment

by:jerw134
ID: 8051920
Using the following code:

Sub Command1_Click()
TheFile = File1.Path + "\" + File1.FileName
WOK = readFile(TheFile, strFile)
strFile = Replace(strFile, "R85.01.01_04", "R85.01.00_04")
Label2.Caption = WOK
End Sub

I get a Compile Error:
ByRef argument type mismatch

What am I doing wrong here?
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8052703
The byref mismatch is caused by your declaration of the strFile variable...
Sub Command1_Click()
 Dinm strFile as string
TheFile = File1.Path + "\" + File1.FileName
WOK = readFile(TheFile, strFile)
strFile = Replace(strFile, "R85.01.01_04", "R85.01.00_04")
Label2.Caption = WOK
End Sub
0
 

Author Comment

by:jerw134
ID: 8052726
Thanks for the responses.  I ended up using something totally different though.

Can I split the points between the two of you?
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8052836
Lol!
Give all points to RanaHossain ...
;-)
Have a nice day, you all
Cesare Imperiali
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question