<-- Previous || Up || Next -->

Replace String In Files Sample Sub
Utilities Class

Private Sub ReplaceStringInFilesSample()
    Const strFn = "ReplaceStringInFilesSample"

    Dim strFromFiles As String
    strFromFiles = CombinePathAndFile(GetWindowsDirectory, "*.*")
    strFromFiles = InputBox("Top of SOURCE file hierarchy: (Wildcards allowed; {Esc} cancels.)", strFn, strFromFiles)
    If Len(strFromFiles) = 0 Then Exit Sub

    Dim strToDirectory As String
    strToDirectory = GetTempFileName(Null, strFn)
    strToDirectory = InputBox("Top of DESTINATION file hierarchy: (ANY FILE WITH THIS NAME WILL BE DELETED.) ({Esc} cancels.)", strFn, strToDirectory)
    If Len(strToDirectory) = 0 Then Exit Sub
    If Len(Dir$(strToDirectory)) Then Kill strToDirectory

    Dim strFindPatt As String
    strFindPatt = GetWindowsDirectory
    strFindPatt = InputBox("Pattern to FIND: ({Esc} cancels.)", strFn, strFindPatt)
    If Len(strFindPatt) = 0 Then Exit Sub

    Dim strReplace As String
    strReplace = Chr$(ASC(strFindPatt) + 1) + Mid$(strFindPatt, 2)
    strReplace = InputBox("REPLACEMENT string: ({Esc} means replacement is blank/empty string.)", strFn, strReplace)

    If Len(strFindPatt) <> Len(strReplace) Then
        If vbCancel = MsgBox("The length of the find and replace strings are different!  This will likely corrupt any binary files!", vbExclamation + vbOKCancel + vbDefaultButton2, strFn) Then Exit Sub
    End If

    Dim enumCompare As VbCompareMethod
    Select Case MsgBox("Use dictionary/text comparisons?  Choose No for literal/binary.", vbQuestion + vbYesNoCancel + vbDefaultButton1, strFn)
        Case vbYes
            enumCompare = vbTextCompare
        Case vbNo
            enumCompare = vbBinaryCompare
        Case vbCancel
            Exit Sub
        Case Else
            Stop
    End Select

    Dim intCapLikeOrig As Integer
    If intCompare = vbBinaryCompare Then
        intCapLikeOrig = True
    Else
        Select Case MsgBox("Capitalize any replacement text as the original text is capitalized?", vbQuestion + vbYesNoCancel + vbDefaultButton1, strFn)
            Case vbYes
                intCapLikeOrig = True
            Case vbNo
                intCapLikeOrig = False
            Case vbCancel
                Exit Sub
            Case Else
                Stop
        End Select
    End If

    Dim strSeparator As String
    strSeparator = InputBox("String separator for exclude files: ({Esc} cancels.)", strFn, " ")
    If Len(strSeparator) = 0 Then Exit Sub

    Dim strExclude As String
    strExclude = "*.mda" + strSeparator + "*.mdb" + strSeparator + "*.mmf" + strSeparator + "*.sec" + strSeparator + "*.zip"
    strExclude = InputBox("Exclude these files: ({Esc} cancels.)", strFn, strExclude)
    If Len(strExclude) = 0 Then Exit Sub

    Dim intReplace As Integer
    Select Case MsgBox("Search and Replace?  Choose No to Search only.", vbQuestion + vbYesNoCancel + vbDefaultButton1, strFn)
        Case vbYes
            intReplace = True
        Case vbNo
            intReplace = False
        Case vbCancel
            Exit Sub
        Case Else
            Stop
    End Select

    MsgBox "This sample subroutine only examines files with Normal attributes; it does NOT examine files which have Hidden and/or System attributes.", vbInformation, strFn
    MsgBox "Return = " & ReplaceStringInFiles(est, strFromFiles, strToDirectory, strFindPatt, strReplace, intCompare, intCapLikeOrig, strExclude, " ", vbNormal, intReplace) & " (True means success; False means failure.)", vbInformation, strFn
    Debug.Print est.MessageText
End Sub

Copyright 1996-1999 Entisoft
Entisoft Tools is a trademark of Entisoft.