Use VBScript scripts to modify the contents of a file, this applies to automated operations _vbs

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.