Jonnyboy Posted July 24, 2008 Posted July 24, 2008 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.ThanksDim FileName, Find, ReplaceWith, FileContents, dFileContentsFind = WScript.Arguments(0)ReplaceWith = WScript.Arguments(1)FileName = WScript.Arguments(2)'Read source text fileFileContents = GetFile(FileName)'replace all string In the source filedFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)'Compare source And resultif dFileContents <> FileContents Then 'write result If different WriteFile FileName, dFileContentsEnd If'Read text filefunction 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 IfEnd 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 ContentsEnd Function Quote
NIM Posted July 24, 2008 Posted July 24, 2008 Could you please be more specific on what you want to do, 'cause I didn't understand you mate Quote
Jonnyboy Posted July 24, 2008 Author Posted July 24, 2008 (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 July 24, 2008 by Jonnyboy Quote
Jonnyboy Posted July 26, 2008 Author Posted July 26, 2008 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. Quote
NIM Posted July 26, 2008 Posted July 26, 2008 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.. Quote
Jonnyboy Posted July 26, 2008 Author Posted July 26, 2008 I might help you do it .vbs anyway, just waiting for the help of my friend.. Cool. Thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.