Use vbscript script to modify the file content, which is applicable to automated operations.

Source: Internet
Author: User

Use vbscript script to modify the file content, which is applicable to automated operations.

'Create a Replace. vbs script. The script content is as follows. When the program is running, enter three parameters: search content, Replace content, and file

Copy codeThe Code is as follows: Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript. Arguments (0)
ReplaceWith = WScript. Arguments (1)
FileName = WScript. Arguments (2)

'Reading files
FileContents = GetFile (FileName)

'Replace all "search 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
'Calculates 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

'Reading files
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

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.