Use VBScript script to modify the contents of a file, this applies to automated operations
' Create a new Replace.vbs script, the script reads as follows, when the program runs, enter three parameters: Find content, replace content, file
Copy Code code as follows:
Dim FileName, Find, ReplaceWith, FileContents, dfilecontents
Find = wscript.arguments (0)
replacewith = wscript.arguments (1)
FileName = WScript.Arguments (2)
' Read the file
FileContents = GetFile (FileName)
Replace all find content in the file with replace content
dfilecontents = replace (filecontents, Find, ReplaceWith, 1,-1, 1)
' Compare source and replace files
If dfilecontents <> filecontents Then
' Save the replaced file
WriteFile FileName, Dfilecontents
WScript.Echo "Replace done."
If Len (replacewith) <> len (find) Then
' Calculate the total number of replacements
WScript.Echo _
((Len (dfilecontents)-Len (filecontents))/(Len (replacewith)-len (Find)) & _
"Replacements."
End If
Else
WScript.Echo "searched string not in the source file"
End If
' Read the file
function GetFile (FileName)
If filename<> "" Then
Dim FS, FileStream
Set FS = CreateObject ("Scripting.FileSystemObject")
On Error Resume Next
Set FileStream = FS. OpenTextFile (FileName)
GetFile = Filestream.readall
End If
End Function
' Write file
function WriteFile (FileName, Contents)
Dim OutStream, FS
On Error Resume Next
Set FS = CreateObject ("Scripting.FileSystemObject")
Set OutStream = FS. OpenTextFile (FileName, 2, True)
Outstream.write Contents
End Function