Jump to content

Recommended Posts

Posted

I could use a little help. I have a script that uses replace and will accept 3 variables to change text in a file. I need to change the script, to regex.replace to accept a (multiline if possible)regular expression as input.

Any help would be appreciated.

Thanks

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, 1)

'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents
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

Posted (edited)
Could you please be more specific on what you want to do, 'cause I didn't understand you mate :(

The script above will take 3 arguments: the filename, the string to change and the replace with string. It will only accept an absolute string, no wildcards. For example:

cscript replace_script.vbs install.rdf 2.0.0.16 3.*

would search install.rdf and replace 2.0.0.16 with 3.*

What I would like to be able to do is:

cscript replace_script.vbs install.rdf "<em:maxVersion>[this string is different in every file]</em:maxVersion>" "<em:maxVersion>3.*</em:maxVersion>"

I have 50+ files with at least 25 different strings to change = 1250 individual iterations to open and search, with regex.replace it would only be 50+ iterations to open and search.

I have found several examples using google, in the form of functions and subs, but nothing that I could make work with arguments.

I have also tried numerous command line utilities, but have not found any that accept regular expressions.

Edited by Jonnyboy
Posted

I have not been able to get this to work properly. I have decided to use gsar.exe. It is blazingly fast, faster that vbscript, anyway.

Thanks for the help.

Posted
I have not been able to get this to work properly. I have decided to use gsar.exe. It is blazingly fast, faster that vbscript, anyway.

Thanks for the help.

I might help you do it .vbs anyway, just waiting for the help of my friend.. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...