• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Precision Search and Replace

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
jerw134
Asked:
jerw134
  • 2
  • 2
1 Solution
 
RanaHossainCommented:
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
 
jerw134Author Commented:
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
 
CimperialiCommented:
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
 
jerw134Author Commented:
Thanks for the responses.  I ended up using something totally different though.

Can I split the points between the two of you?
0
 
CimperialiCommented:
Lol!
Give all points to RanaHossain ...
;-)
Have a nice day, you all
Cesare Imperiali
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now