I need to write a .bat/.cmd/.vbs file that changes the text of a text file and I also need regex terms.
I have the following text file as .txt.
"Bild/Print/59/00-Einstiegsbild-neu_59115.jpg" -resize 227.05x227.05%% -rotate -0 -shear 0x0 -crop 2011x1051+104+328 "web\00-Einstiegsbild-neu_59115.jpg"
"Bild/Print/59/01-Zwischenbild-neu_59150.jpg" -resize 100.39x100.39%% -rotate -0 -shear 0x0 -crop 2012x988+0+82 "web\01-Zwischenbild-neu_59150.jpg"
Now I want to do the following regex search and replace:
(1. Replace)
Search: "
Replace: (nothing)
(2. Replace)
Search: .+(?=web)
Replace: (nothing)
Now the text should be:
web\00-Einstiegsbild-neu_59115.jpg
web\01-Zwischenbild-neu_59150.jpg
(3. Replace)
Search: web\\
Replace: E:\K4_XML_Export\tpx_K4toWP_ImageMagick\web\
which should result in:
E:\K4_XML_Export\tpx_K4toWP_ImageMagick\web\00-Einstiegsbild-neu_59115.jpg
E:\K4_XML_Export\tpx_K4toWP_ImageMagick\web\01-Zwischenbild-neu_59150.jpg
Since I have absolutely no idea about batch files I hope you can help me further or share certain approaches or considerations.
Thank you in advance for the feedback and best regards
Noel
What I already tested – I know how to change text like red to blue with:
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "E:\imglist_2.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"red")> 0 Then
strLine = Replace(strLine,"red","blue")
End If
WScript.Echo strLine
Loop
cscript /nologo E:\test.vbs > newfile
ren newfile file.txt
I hope this helps... Regular expression can skip a few steps ahead and just grab the string (web...) without needing the redundant steps. Resulting in the desired pattern output.
Please note you'll need to update this version back to your file strFile = "E:\imglist_2.txt"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFS, objFile, PrimaryPath, strFile
PrimaryPath="E:\K4_XML_Export\tpx_K4toWP_ImageMagick"
strFile = "imagelist.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile, ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"web")> 0 Then ImagePath = PrimaryPath & "\" & GetWebImg(strLine)
WScript.Echo ImagePath
Loop
Function GetWebImg(str)
Set RE = CreateObject("VBScript.RegExp")
RE.Global = True
RE.Pattern = "(web[^""]*)"
Set matches = RE.Execute(str)
If matches.count > 0 Then
GetWebImg=matches(0)
End If
End Function
Related
I need to run a VBScript that changes the structure of a CSV file. To keep it simple I'm only using 3 data fields but there is a lot more. In a production environment I will have a CSV file with hundreds of lines.
The problem is everything is in double quotes. The end result can sometimes be no quotes or single quotes or sometimes a mix of all three.
I have absolutely no idea how I should approach this and was looking for some guidance. This looks like a job for RegexReplace but because it's mixed I'm not sure how to start this. After the file has been modified I have to right over top of the original file.
CSV Example:
"apple";"12";"xyz"
"somereallylongword";"7687";"theredfox"
Pattern
"%1";%2;'%3'
Desired Result
"apple";12;'xyz'
"somereallylongword";7687;'theredfox'
What I'm trying to achieve is to be able to make a new pattern type. In my example:
"%1" - I keep the original double quotes.
%2 - Remove the double quotes.
'%3' - Replace the double quotes with single quotes.
Any insight would be greatly appreciated.
You can read the CSV file using ADODB:
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Dim objConnection
Dim objRecordset
Dim sCSVFolder
Dim sCSVFile
Dim sValue
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sCSVFolder = "C:\CSV_Folder\"
sCSVFile = "your_csv_file.csv"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sCSVFolder & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
objRecordset.Open "SELECT * FROM " & sCSVFile, _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF
' Modify and write fields to new text file here
sValue = objRecordset.Fields.Item("FieldName")
objRecordset.MoveNext
Loop
This way you let ADO handle reading the data and removing the double-quotes and you can manipulate the data easily as a Recordset.
Just give a try for this code by replacing the path of your CSV file and tell me how it works on your side ?
Option Explicit
Dim Data
Call ForceCScriptExecution()
Data = ReadFile("C:\Test\Test.csv")
wscript.echo "Before Replacing"
wscript.echo String(50,"-")
wscript.echo Data
wscript.echo String(50,"-")
wscript.echo "After Replacing"
wscript.echo String(50,"-")
wscript.echo Search_Replace(Data)
wscript.echo String(50,"-")
wscript.sleep 20000
'-----------------------------------------------
Function Search_Replace(Data)
Dim oRegExp,strPattern1,strPattern2
Dim strReplace1,strReplace2,strResult1,strResult2
strPattern1 = ";(\x22)(\S+\w+)(\x22);"
strReplace1 = ";$2;"
strPattern2 = "[;]\x22([^\x22]+)\x22"
strReplace2 = ";'$1'"
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.IgnoreCase = True
oRegExp.Pattern = strPattern1
strResult1 = oRegExp.Replace(Data,strReplace1)
oRegExp.Pattern = strPattern2
strResult2 = oRegExp.Replace(strResult1,strReplace2)
Search_Replace = strResult2
End Function
'-----------------------------------------------
Function ReadFile(path)
Const ForReading = 1
Dim objFSO,objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path,ForReading)
ReadFile = objFile.ReadAll
objFile.Close
End Function
'----------------------------------------------
Sub ForceCScriptExecution()
Dim Arg, Str, cmd, Title
Title = "Search and Replace using RegExp by Hackoo 2019"
cmd = "CMD /C Title " & Title &" & color 0A & Mode 80,30 & "
If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
For Each Arg In WScript.Arguments
If InStr( Arg, " " ) Then Arg = """" & Arg & """"
Str = Str & " " & Arg
Next
CreateObject( "WScript.Shell" ).Run _
cmd & "cscript //nologo """ & _
WScript.ScriptFullName & _
""" " & Str
WScript.Quit
End If
End Sub
'-----------------------------------------------
Edit : Batch Script Code
You can do it easily with a batch script without using Regex :
#echo off
Title Edit CSV File
Set "Input_CSV_File=C:\Test\Test.csv"
Set "OutPut_CSV_File=C:\Test\OutPut_Test.csv"
If Exist "%OutPut_CSV_File%" Del "%OutPut_CSV_File%"
#for /f "tokens=1,2,3 delims=;" %%a in ('Type "%Input_CSV_File%"') Do (
echo "%%~a";%%~b;'%%~c'
echo "%%~a";%%~b;'%%~c'>>"%OutPut_CSV_File%"
)
TimeOut /T 5 /NoBreak>nul
If Exist "%OutPut_CSV_File%" Notepad "%OutPut_CSV_File%" & Exit
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
I have a string like this "f_details('277095');">. I just need to get the 277095 part. I've been trying variations of strPattern = "'[0-9]'+", but this is either finding nothing or finding the wrong things.
I don't understand regular expressions despite having a cheat sheet right in front of me. Spent an hour trying different things already. What would this regexp look like?
Here is my code that I use to scrape this site and grab data:
Set objWshShell = Wscript.CreateObject("Wscript.Shell")
Set IE = CreateObject("internetexplorer.application")
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
For i=1 To 77 '77 Counties
If i=77 Then Exit For
IE.Visible = True
IE.Navigate "https://lic.ok.gov/PublicPortal/OREC/FindAssociateEntity.jsp"
Do Until IE.ReadyState = 4: WScript.sleep 15: Loop
Do Until IE.Document.ReadyState = "complete": WScript.sleep 10: Loop
IE.Document.getElementsByTagName("select")("AddrCountyCode").Value = i
Do Until IE.Document.ReadyState = "complete": WScript.sleep 10: Loop
For Each btn In IE.Document.getElementsByTagName("input")
If btn.name = "btnSearch" Then btn.Click()
NEXT
strPattern = "'(\d+)'"
strTestString = ie.document.body.innerhtml
arrAllMatches = fGetMatches(strPattern, strTestString)
If UBound(arrAllMatches) <> 0 Then
filename = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\License.txt"
set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile(filename,8,true)
ts.write Join(arrAllMatches, vbCrlf)
ts.close
Else
WScript.Echo "-- None Found --"
End if
next
Wscript.echo "DONE!"
'=====================================================================
Function fGetMatches(sPattern, sStr)
Dim regEx, retVal, sMatch, colMatches, temp
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set colMatches = regEx.Execute(sStr) ' Execute search.
If colMatches.Count = 0 Then
temp = Array("")
Else
'# Convert Collection to Array
For Each sMatch In colMatches
temp = temp & sMatch & "¶"
Next
temp = Left(temp, Len(temp) - 1)
temp = Split(temp, "¶")
End If
fGetMatches = temp
End Function
'\d+'
Just add quantifier to \d instead of ' as you want \d to repeat.
Try (?<=')\d+(?=') if you want to get only 277095
See demo.
https://regex101.com/r/iS6jF6/6
Dim strRegex as String = "'\d+'"
Dim myRegex As New Regex(strRegex, RegexOptions.Multiline)
Dim strTargetString As String = "f_details('277095');"
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
' Add your code here
End If
Next
VBScript's regexp implementation is restricted, but if you follow the general rule "Keep it simple", even here you can cut a sequence of numbers easily:
>> Set r = New RegExp
>> r.Pattern = "\d+"
>> s = "f_details('277095');"
>> WScript.Echo r.Execute(s)(0).Value
>>
277095
Additionaly to Vks answer, you can use capturing groups to capture the content you need.
You can use a regex like this:
'(\d+)'
Working demo
You can see highlighted in blue the match and in green the captured content
Match information
MATCH 1
1. [11-17] `277095`
I am trying to figure out how to use vbscript to:
1 - open a .csv file as a .txt file
2 - search for a certain string of text that is located randomly throughout the text
3 - replace that string with a different string.
I have found an article that helped me learn how to replace an entire line in a .txt document, but so far have had no luck finding anything about replacing just certain characters within the line.
Thanks!
Here is the code I am using currently:
Const ForReading = 1
Const ForWriting = 2
'Setting up our objects and focusing on the text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If strLine = "Myer" Then
strLine = "Mike"
End If
strContents = strContents & strLine & vbCrLf
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForWriting)
objFile.Write(strContents)
objFile.Close
The text file it references says:
Ken Myer
Fabrikam
Pilar Ackerman
Wingtip Toys
Jeff Hay
Fabrikam
Ellen Adams
Northwind Traders
Myer
(End of text file). So essentially, I have gotten the code to successfully change the "Myer" that is on its own line to "Mike". What I am having a hard time with is changing the "Myer" in the first line to "Mike". Hopefully this helps clarify things a bit...I'm extremely new at this so not sure of the language I should be using to describe the problem.
Use Replace on the file's content obtained by .ReadAll() and .Write the result back. In code:
Option Explicit
Dim goFS : Set goFS = Createobject("Scripting.FileSystemObject")
Dim goWAU : Set goWAU = WScript.Arguments.Unnamed
WScript.Quit main()
Function main()
main = 1 ' assume error
If 3 = goWAU.Count Then
If goFS.FileExists(goWAU(0)) Then
Dim s : s = goFS.OpenTextFile(goWAU(0)).ReadAll()
If 0 < Instr(s, goWAU(1)) Then
goFS.CreateTextFile(goWAU(0)).Write Replace(s, goWAU(1), goWAU(2))
WScript.Echo "done"
main = 0
Else
WScript.Echo goWAU(1), "not found"
End If
Else
WScript.Echo goWAU(0), "does not exist"
End If
Else
WScript.Echo "need 3 args: fspec, find, replacement"
End If
End Function
output:
copy con 28350055.csv
1,2,3
4,5,6
^Z
cscript 28350055.vbs 28350055.csv 5 4711
done
type 28350055.csv
1,2,3
4,4711,6
cscript 28350055.vbs 28350055.csv 5 4711
5 not found
cscript 28350055.vbs 28350055.cs 5 4711
28350055.cs does not exist
Use that demo to determine what is needed to solve your real world problem.
I'm extremly new too, so i didnt get what the other answer do in the code, but i figured out in the last answer about "Replace" and use in your code for do what you need, and the result is this:
Const ForReading = 1
Const ForWriting = 2
'Setting up our objects and focusing on the text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
strLine = Replace(strLine,"Myer","Mike")
' If strLine = "Myer" Then
' strLine = "Mike"
' End If
strContents = strContents & strLine & vbCrLf
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("F:\BIBLIOTECAS\Archivos\TEST.txt", ForWriting)
objFile.Write(strContents)
objFile.Close
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