forked from tomlogic/pinmame2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleReplace.wsf
executable file
·237 lines (195 loc) · 7.83 KB
/
SimpleReplace.wsf
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<job>
<resource id="scripttitle">Simple Replace v1.20 by Matthias "Maddes" Buecher (c) 2003-2010</resource>
<comment>
Developed with Windows Scripting Host (WSH) 5.6 and VBScript 5.6.7426 under Windows XP SP1
Succesfully tested under Windows 98 SE (which has all MS recommended updates installed)
WSH/VBScript updates and documentation are available for download at http://msdn.microsoft.com/scripting/
Contact: http://www.maddes.net/
Copyright (C) 2003-2011 Matthias "Maddes" Buecher
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
http://www.gnu.org/licenses/gpl-2.0.txt
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
</comment>
<runtime>
<description></description>
<named name="in" type="string" required="true" helpstring="Input file" />
<named name="out" type="string" required="false" helpstring="Output file" />
<named name="search" type="string" required="true" helpstring="Search phrase" />
<named name="replace" type="string" required="true" helpstring="Replace phrase" />
<named name="dblslash" type="simple" required="false" helpstring="Converts \ to \\ in search and replace phrase" />
<named name="hex" type="simple" required="false" helpstring="Converts replace phrase in a byte row for registry files" />
<named name="quotes" type="simple" required="false" helpstring="Converts ^' to a quote in search and replace phrase" />
<named name="searchquotes" type="simple" required="false" helpstring="Converts ^' to a quote in search phrase" />
<named name="replacequotes" type="simple" required="false" helpstring="Converts ^' to a quote in replace phrase" />
<example>Also check out the included text file with lots of more information</example>
</runtime>
<script language="VBScript">
Option Explicit 'Only defined variables can be used
Dim vScriptTitle, vDisplayString
Dim vVersion
Dim vInfoString, vEngError
Dim oArgNamed, oArgUnnamed, vArgError
Dim vSearchString, vReplaceString
Dim vInFile, vOutFile
Dim vDblSlash
Dim vHexConvert, i, vReplaceStringTmp
Dim vSearchQuotes, vReplaceQuotes
' ScriptTitle for start information
vScriptTitle = getResource("scripttitle")
vDisplayString = vScriptTitle & vbNewLine _
& "This script comes with ABSOLUTELY NO WARRANTY." & vbNewLine _
& "This is free software, and you are welcome to redistribute it under certain conditions; see GPL.TXT for details." & vbNewLine
' Create ScriptEngine information string
vInfoString = "This Script was developed with Windows Scripting Host (WSH) 5.6" & vbNewLine _
& "and VBScript 5.6.7426 under Windows XP SP1." & vbNewLine
vInfoString = vInfoString & vbNewLine _
& "You are currently using WSH " & WScript.Version _
& " and " _
& ScriptEngine & " Version " _
& ScriptEngineMajorVersion & "." _
& ScriptEngineMinorVersion & "." _
& ScriptEngineBuildVersion & vbNewLine
vInfoString = vInfoString & vbNewLine _
& "Updates and documentation are available" & vbNewLine _
& "for download at http://msdn.microsoft.com/scripting/"
' Check runtime versions
vEngError = 0
' Check WSH version (5.6 because of named/unnamed parameters, <runtime>, <named>, <resource>, <example>, ShowUsage)
vVersion = Split(WScript.Version, ".")
if (vVersion(0) < 5) or (vVersion(0) = 5 and vVersion(1) < 6) then
vInfoString = vInfoString & vbNewLine _
& vbNewLine & "You HAVE to update to WSH 5.6, if you WANT to use THIS script."
vEngError = 1
end if
' Check VBS version (2.0 because of const, replace function, etc.)
if ScriptEngineMajorVersion < 2 then
vInfoString = vInfoString & vbNewLine _
& vbNewLine & "You HAVE to update to VBS 2.0, if you WANT to use THIS script."
vEngError = 1
end if
' Exit on version errors
if vEngError > 0 then
vDisplayString = vDisplayString & vbNewLine & vInfoString & vbNewLine
WScript.Echo(vDisplayString)
WScript.Quit(1)
end if
' Parameter objects
Set oArgNamed = WScript.Arguments.Named
Set oArgUnnamed = WScript.Arguments.Unnamed
' Check parameters
vArgError = 0
' Check input and output file paramters
vInFile = ""
if oArgNamed.Exists("in") then
vInFile = oArgNamed("in")
end if
vOutFile = ""
if oArgNamed.Exists("out") then
vOutFile = oArgNamed("out")
end if
' Check search and replace paramters
vSearchString = ""
if oArgNamed.Exists("search") then
vSearchString = oArgNamed("search")
' check for own escape sequence for double quotes
vSearchString = Replace (vSearchString, "``", """")
end if
vReplaceString = ""
if oArgNamed.Exists("replace") then
vReplaceString = oArgNamed("replace")
' check for own escape sequence for double quotes
vReplaceString = Replace (vReplaceString, "``", """")
end if
' Check reg parameter
vDblSlash = oArgNamed.Exists("dblslash")
vHexConvert = oArgNamed.Exists("hex")
' Check quotes parameter
vSearchQuotes = oArgNamed.Exists("searchquotes")
if not vSearchQuotes then
vSearchQuotes = oArgNamed.Exists("quotes")
end if
vReplaceQuotes = oArgNamed.Exists("replacequotes")
if not vReplaceQuotes then
vReplaceQuotes = oArgNamed.Exists("quotes")
end if
' Missing parameters
if vInFile = "" then
vDisplayString = vDisplayString & vbNewLine & "Error: You have to state an input file with /in" & vbNewLine
vArgError = 1
elseif vInFile = vOutFile then
vDisplayString = vDisplayString & vbNewLine & "WARNING: Input and output file are the same" & vbNewLine
end if
if vSearchString = "" then
vDisplayString = vDisplayString & vbNewLine & "Error: You have to state a search phrase" & vbNewLine
vArgError = 1
end if
' Convert search and replace string for registry files
if vDblSlash then
vSearchString = Replace(vSearchString, "\", "\\")
vReplaceString = Replace(vReplaceString, "\", "\\")
end if
if vHexConvert then
vReplaceStringTmp = ""
for i = 1 to len(vReplaceString)
if i > 1 then
vReplaceStringTmp = vReplaceStringTmp + ","
end if
vReplaceStringTmp = vReplaceStringTmp + Hex(Asc(Mid(vReplaceString,i,1)))
next
vReplaceString = vReplaceStringTmp
end if
' Convert search and replace string due to quotes in strings and WSH's quote issue
if vSearchQuotes then
vSearchString = Replace(vSearchString, "^'", """")
end if
if vReplaceQuotes then
vReplaceString = Replace(vReplaceString, "^'", """")
end if
if vArgError = 0 then
vDisplayString = vDisplayString & vbNewLine & _
"Reading """ & vInFile & """, replacing """ & vSearchString & """ with """ & vReplaceString & """"
if not vOutFile = "" then
vDisplayString = vDisplayString & ", writing """ & vOutFile & """"
end if
vDisplayString = vDisplayString & vbNewLine
end if
' Display start information
WScript.Echo(vDisplayString)
vDisplayString = ""
' Exit on argument errors
if vArgError > 0 then
WScript.Arguments.ShowUsage
WScript.Quit(1)
end if
' NOW WE ARE GOOD TO GO...
Dim oFSO, oFileIn, oFileOut, vText
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
if not oFSO.FileExists(vInFile) then
vDisplayString = vbNewLine & "Error: Input file """ & vInFile & """ not found" & vbNewLine
WScript.Echo(vDisplayString)
WScript.Quit(1)
end if
Set oFileIn = oFSO.OpenTextFile(vInFile, 1, false)
vText = oFileIn.ReadAll()
oFileIn.Close()
vText = Replace(vText, vSearchString, vReplaceString)
if not vOutFile = "" then
Set oFileOut = oFSO.CreateTextFile(vOutFile, true)
oFileOut.Write(vText)
oFileOut.Close()
else
WScript.Echo(vText)
end if
WScript.Echo("Done.")
WScript.quit(0)
</script>
</job>