Function File Operations Class

ReplaceStringInFile

Combination file copy operation with text replacement. All occurrences of some string are replaced by another string. Function returns True upon success and False upon failure.

Signature (Visual Basic / VBA)

Public Function ReplaceStringInFile( _
      ByVal vFromFile As Variant _
    , ByVal vToFile As Variant _
    , ByVal vFromString As Variant _
    , Optional ByVal vToString As Variant _
    , Optional ByVal vCompare As Variant _
    , Optional ByVal vCapitalizeLikeOriginal As Variant _
    ) As Boolean

Code Example

    StringToFile("This is a test.", "Temp.In") = True
    ReplaceStringInFile("Temp.In", "Temp.Out", "t" "tt") = True
    FileToString("Temp.In") = "Tthis is a ttestt."

Parameters

Parameter Description
vFromFile Name of the input file. Function does nothing and returns False if vFromFile is Null or cannot be fixed-up to a String.
vToFile Name of the output file. Function does nothing and returns False if vToFile is Null or cannot be fixed-up to a String. If vToFile already exists, it will be overwritten.
vFromString The string that is to be replaced wherever it occurs within the file. Function does nothing and returns False if vFromString is Null or cannot be fixed-up to a String. Function also returns False if vFromString is an empty String ("").
vToString The string that is to replace vFromString. vToString defaults to an empty String ("") if it is missing or Null or cannot be fixed-up to a String. If vToString is an empty String, then vFromString will be removed (stripped) from the file.
vCompare Type of comparison used to determine if strings match. vCompare defaults to Binary comparisons if it is missing or Null or cannot be fixed up to a number.
vCapitalizeLikeOriginal True if the replacement string should be capitalized the same way as the string that it is replacing (as defined by the CapitalizeLike function), or False if the replacement string should be used as-is. vCapitalizeLikeOriginal defaults to True if it is missing or Null or cannot be fixed-up to a number.

Notes & Version History

Note Use the Visual Basic FileCopy function or the Entisoft Tools CopyFile function if you have no need for String replacement.