' Intellectual replacement of word in all files in directory - ' if source string is already part of destination string, replacement isn't performed. ' Standard usage is changing built-in function name to custom function name e.g. set_window to my_set_window Public strFileName, strFrom, strTo, strScriptPath, response, fso Set fso = CreateObject("Scripting.FileSystemObject") ' Create a FileSystemObject. Call set_params ' set replacement parameters Call main ' execute replacement Sub set_params() strFileName = InputBox("What files to search", "Files to search", "script") ' file name If strFileName = "" Then Exit Sub strFrom = InputBox("Find what", "Replace", "set_window") ' source string If strFrom = "" Then Exit Sub strTo = InputBox("Replace with", "Replace", "my_set_window") ' destination string ' strScriptPath = fso.GetParentFolderName(Wscript.ScriptFullName) ' get curent directory strScriptPath = "..\Regression_Tests" strScriptPath = InputBox("Search directory", "Directory", strScriptPath) If strScriptPath = "" Then Exit Sub response=msgbox ("Script will replace '" & strFrom & "' to '" & strTo & "'" & chr(10) & chr(13) & "in all files '" &_ strFileName & "' under '" & strScriptPath & "' directory", vbExclamation+vbOKCancel, "Read carefully!!!!") End Sub Sub main() If strFileName = "" Or strFrom = "" Or strScriptPath = "" Or response = 2 Then MsgBox "Script was canceled", vbInformation Exit Sub End If lenFrom = Len(strFrom) ' source lenghth lenTo = Len(strTo) ' destination lenght intFiles = 0 ' No of processed files intCount = 0 ' No of replacements intCountBefore = 0 ' No of replacements for file intPass = 0 ' No of skupped replacements (source is part of destination already) strCmd = "dir "&strScriptPath&"\"&strFileName&" /S /B >"&strScriptPath&"\fileslist.tmp" ' get list of files using DOS command Set WshShell = CreateObject("WScript.Shell") ' Create a WshShell object... WshShell.Run "%comspec% /c " & strCmd, 0, True ' ...and shell an MS-DOS command ' The MS-DOS command is finished. A temporary file ' now exists containing the output of the dir command Set FileList = fso.OpenTextFile(strScriptPath&"\fileslist.tmp") ' list of files Do While Not (FileList.atEndOfStream) curFileName = FileList.ReadLine ' get one intFiles = intFiles + 1 Set Files = fso.OpenTextFile(curFileName) ' open selected file intCountBefore = intCount Set Filet = fso.OpenTextFile("script.tmp", 2, True) Do While Not (Files.atEndOfStream) ' for each line in selected file curLine = Files.ReadLine toLine = "" Do intFrom = InStr(curLine, strFrom) ' get position of source word intTo = InStr(curLine, strTo) ' get position of destination word If intFrom = 0 Then ' if not found toLine = toLine & curLine ' catenate all (left) line as is Else ' if found source is part of destination, replacement will not applied If intTo <> 0 And intFrom >= intTo And intFrom <= (intTo + lenTo) And (intFrom + lenFrom) <= (intTo + lenTo) Then toLine = toLine & Mid(curLine, 1, intTo + lenTo - 1) ' catenate processed part (including destination word) as is curLine = Mid(curLine, intTo + lenTo) ' left only unprocessed part of source string intPass = intPass + 1 Else toLine = toLine & Mid(curLine, 1, intFrom - 1) & strTo ' if found source isn't part of destination, curLine = Mid(curLine, intFrom + lenFrom) ' replace word intCount = intCount + 1 End If End If Loop While intFrom <> 0 Filet.WriteLine toLine Loop Files.Close ' close files Filet.Close If intCount > intCountBefore Then ' if there were changes for file fso.DeleteFile curFileName ' replace source file with processed temporary file fso.MoveFile "script.tmp", curFileName Else fso.DeleteFile "script.tmp" ' else simply delete temporary target file End If Loop FileList.Close ' Close Dir found files fso.DeleteFile strScriptPath&"\fileslist.tmp" ' Delete file. msgbox intCount & " replacements were applied," & chr(10) & chr(13) & intPass & " replacements were skipped" &_ chr(10) & chr(13) & "for " & intFiles & " files" &_ Chr (10) & Chr(13) & "Check SRP_Functions file!", vbInformation, "Replacement is finished" End Sub