VBS RegEx Search and Replace - regex

I am attempting to use a vbs script to replace the limit value in a .ini file that contains the following line:
CC_refund_limit=####.##
Unfortunately the ####.## can be any dollar value. I am replacing with a standard:
CC_refund_limit=500.00
I have attempted the following with every variation of \d \d+ enclosed in every bracket known to mankind and have even attempted [0-999] on a test file. The only way I can get this to work has been with the following code and only if the string contains an actual dollar value as written. I have over 1600 instances on as many different servers to replace this on.
Any guidance would be appreciated:
`Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\EDC\Edc.ini", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "CC_refund_limit=200.00", "CC_refund_limit=500.00")
Set objFile = objFSO.OpenTextFile("C:\EDC\Edc.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close
`

Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\EDC\Edc.ini", ForReading)
strText = objFile.ReadAll
objFile.Close
Set r = New Regexp
With r
.pattern = "CC_refund_limit=\d+\.\d+"
.global = true
End with
StrNewText = r.Replace(strText,"CC_refund_limit=500.00")
Set objFile = objFSO.OpenTextFile("C:\EDC\Edc.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Related

VBS How to replace any color string (#******) with new value

trying to replace in a folder different color values with a new one this is my starting code:
Option Explicit
Dim objFSO, strFolder, objFolder, objFile
Dim strOldValue, strNewValue, objRead, strContents, objWrite
Const ForReading = 1
Const ForWriting = 2
strFolder = "..\..\chrome\OPCEN\TABS"
strOldValue = "#******"
strNewValue = "#F5F5F5"
UPDATE: this is the working code using RegEx (but is pointing to the file not to the whole folder)
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("..\..\chrome\OPCEN\TABS\tabs.css", ForReading)
strText = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = False
objRegEx.Pattern = "\#[A-Z 0-9]{6}"
strNewText = objRegEx.Replace(strText, "#F5F5F5")
Set objFile = objFSO.OpenTextFile("..\..\chrome\OPCEN\TABS\tabs.css", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Use For Each loop to processing files and Regular Expressions to replace your strings.
Try my way :
Const ForReading = 1
Const ForWriting = 2
Set fso=Createobject("Scripting.FileSystemObject")
Set objRegEx = New RegExp
FolderName = "..\..\chrome\OPCEN\TABS"
Set objFolder = fso.GetFolder(FolderName)
Set objFileCol = objFolder.Files
For Each obFile In objFileCol
If Right(obFile,3)="css" Then 'Check all files extensions.
Set objFile = fso.OpenTextFile(obFile, ForReading)
strText = objFile.ReadAll
objFile.Close
objRegEx.Global = True
objRegEx.IgnoreCase = False
objRegEx.Pattern = "\#[A-Z 0-9]{6}"
strNewText = objRegEx.Replace(strText, "#F5F5F5")
Set WriteFile = fso.OpenTextFile(obFile, ForWriting)
WriteFile.WriteLine strNewText
WriteFile.Close
End if
Next

vbscript use regex to rename files recursively

I am trying to write a vbscript to recursively rename any files in a folder.
My final plan will be to copy the folder to a zip file, but it fails if the files have any characters apart from
e.g (a-z) or (0-9) or "_", " ".
For example, if the file has characters other than English in the file name, it will not allow me to copy the file to the zip. I have looked at various websites and could have an answer in batch as well.
I am quite new to regex, having started learning about it yesterday.
The error I am getting is "File already Exists".
My script:
Const ForReading = 1
Const ForWriting = 2
Const ForAppend = 8
Const OverwriteExisting = TRUE
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = scriptdir & "\Fragments"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "[^A-Za-z_0-9-\n\r]"
For Each objFile in colFiles
If regEx.test(objFile.Name) = true Then
FirstlevelNewFileName = objRegEx.Replace(objFile.Name, "_")
objFSO.MoveFile objFile, FirstlevelNewFileName
End If
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
NewFileName = objRegEx.Replace(objFile.Name, "_")
objFSO.MoveFile objFile.Path, NewFileName
Next
ShowSubFolders Subfolder
Next
End Sub
Thank you for your time!
I think your code have got issues with the Movefile statements(when you are trying to rename files). You are providing only the file name as the Destination parameter. You should provide full path there. I have made changes as shown below.
NOTE: I did not change any of the Logic.
Const ForReading = 1
Const ForWriting = 2
Const ForAppend = 8
Const OverwriteExisting = TRUE
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = scriptdir & "\Fragments"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
Set objRegEx = new Regexp
objRegEx.Global = True
objRegEx.Pattern = "[^\w.]" 'looks for non-word characters and also not .(for the extension)
For Each objFile in colFiles
If objRegEx.test(objFile.Name) = true Then 'Changed this. You had only used the variable RegEx here instead of objRegEx
FirstlevelNewFileName = objRegEx.Replace(objFile.Name, "_")
objFile.Move objStartFolder&"\"&FirstlevelNewFileName 'Provided the full file path here. Used the File Object itself
EndIf
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
NewFileName = objRegEx.Replace(objFile.Name, "_")
objFile.Move Subfolder.Path&"\"&NewFileName 'passed the full path here again
Next
ShowSubFolders Subfolder
Next
End Sub

Find and replace with a regular expression in a text file

I am building a jenkins jobs for build comparison. I want to replace double newline (\n\n) in the text file with single new line (\n). After that I want to replace every instance of word "commit" with a newline and commit i.e "\ncommit". I want to use a vbscript for this, anyone can suggest how to do this?
Currently I am using following VBScript:
Const ForReading = 1
Const ForWriting = 2
strName = Wscript.Arguments(0)
strOriginal = Wscript.Arguments(1)
strReplacement = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strName, ForReading)
strText = objFile.ReadAll
objFile.Close
' Replace desired string
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.IgnoreCase = False
objRegExp.Pattern = strOriginal
strReplacement = objRegExp.Replace(strText, strReplacement)
Set objFile = objFSO.OpenTextFile(strName, ForWriting)
objFile.Write strReplacement
objFile.Close
The script replaces regex patterns with a normal string, i.e. it replaces double newline with a normal string i.e with string \n. I don't know how to extend this to replace regex with regex.
Regular expressions are overkill for this. Use two string replacements:
strText = objFSO.OpenTextFile(strName, ForReading).ReadAll
strText = Replace(strText, vbLf & vbLf, vbLf)
strText = Replace(strText, "commit", vbLf & "commit")
objFSO.OpenTextFile(strName, ForWriting).Write strText

Regex pattern not working in VB Script

I have this VBS code:
Option Explicit
Dim reMethod, reInterface
Dim vaction
Dim fileService
Dim mService
Dim lineService
Dim objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fileService = objFSO.OpenTextFile("GIISAssuredController.java" , ForReading)
Set reMethod = new regexp
reMethod.Pattern = """\w+?""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
reMethod.IgnoreCase = True
reMethod.Global = True
Do Until fileService.AtEndOfStream
lineService = fileService.ReadLine
For Each mService In reMethod.Execute(lineService)
vaction = mService.Submatches(0)
Set reInterface = new regexp
Wscript.Echo vaction
Next
Loop
And 'GIISAssuredController.java':
} else if ("hello".equals(ACTION)) {
Integer assuredNo = giisAssuredService.saveAssured(assured);
The regex pattern is not working.
I am expecting the output to be is:
saveAssured
But instead, it's not echoing anything. I tried the regex pattern here > https://regex101.com/r/kH3aZ4/1, and it's getting the 'saveAssured' string.
This question is related to: Multiline REGEX using VB Script
If the expression needs to match a text that spawns over multiple lines, but you read the file line by line and test line by line, there will never be a match.
Option Explicit
Const ForReading = 1
Dim code
code = CreateObject("Scripting.FileSystemObject" _
).OpenTextFile("GIISAssuredController.java" , ForReading _
).ReadAll()
Dim mService
With new RegExp
.Pattern = """\w+?""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
.IgnoreCase = True
.Global = True
For Each mService in .Execute(code)
WScript.Echo mService.Submatches(0)
Next
End With

vb script if statement

I want to edit a file with VB if the word that must to be in file isn't exist.
when execute this file, I want if condition was true do nothing but all file content was erase.
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForReading)
strText = objFile.ReadAll
objFile.Close
strSearchFor = "this word must to exist"
If InStr(1, strText, strSearchFor) > 0 then
'do nothing
else
strNewText = Replace(strText,"this word must to delete","this word must to exist" )
End If
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForWriting)
objFile.WriteLine strNewText
objFile.Close
that becouse strNewText will be null when your condition true. and still replace the strText with your empty strNewText.
Keep your works in side the if(). so it will solve the Problem.
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForReading)
strText = objFile.ReadAll
objFile.Close
strSearchFor = "this word must to exist"
If InStr(1, strText, strSearchFor) > 0 then
'do nothing
else
strNewText = Replace(strText,"this word must to delete","this word must to exist" )
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForWriting)
objFile.WriteLine strNewText
objFile.Close
End If