-
Notifications
You must be signed in to change notification settings - Fork 119
/
replace.vbs
56 lines (48 loc) · 1.63 KB
/
replace.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' [cscript|wscript] replace.vbs Find Replacewith File
''
'' Find … Required. Substring being searched for.
'' Replacewith … Required. Replacement substring.
'' File … Source and destination file for the replacement
''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'Read source text file
FileContents = GetFile(FileName)
'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 0)
'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents
'Wscript.Echo "Replace done."
'If Len(ReplaceWith) <> Len(Find) Then 'Can we count n 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 text 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 string As a text 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