How to use vbscript script to modify File content
This article mainly explains "how to use vbscript script to modify file content", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use vbscript script to modify file content" bar!
Use vbscript script to modify file contents, which is suitable for automated operations.
'create a new Replace.vbs script with the following contents. Enter three parameters when the program is running: find content, replace content, and file
The copy code is 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 the source file with the replaced file
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 substitutions
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 a 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
Thank you for your reading, the above is the content of "how to use vbscript script to modify file content", after the study of this article, I believe you have a deeper understanding of how to use vbscript script to modify file content, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!