Log inRegister
You are here: Boossy » RegularExpressions

Regular expressions

Use Case: bankdocumenten hernoemen

'========================================================
' VBScript to rename bank documents so they appear in
' the right order
' Derived from script written by ApOgEE: https://coderstalk.blogspot.com/2007/09/vbscript-to-replace-underscores-in.html?m=0
' RegEx variable aantal cijfers: https://stackoverflow.com/questions/12011792/regular-expression-matching-a-3-or-4-digit-cvv-of-a-credit-card
' regex replace script: https://www.devguru.com/content/technologies/VBScript/regexp-replace.html
'========================================================
Dim sName
Dim fso
Dim fol
' voor de regular expression(s)
Dim RegX
Set RegX = NEW RegExp

On Error Resume Next

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' get current folder
Set fol = fso.GetFolder(".")

' go thru each files in the folder
For Each fil In fol.Files
   If InStr(1, fil.Name, "TroepDatWegMag_") <> 0 Then
      ' rename
      sName = Replace(fil.Name, "TroepDatWegMag_", "")
      ' regex zoekpatroon: underscore, 4 of 5 cijfers, dash, 3 cijfers, 7 of 8 cijfers 
      ' (neemt in dat laatste geval het hoogste aantal blijkbaar)
      SearchPattern = "_\d{4,5}-\d{3}_\d{7,8}"
      ReplaceString = "_bankafschrift"
      RegX.Pattern = SearchPattern
      RegX.Global = True
      ReplacedText = RegX.Replace(sName, ReplaceString)
      ' rename the file
      fil.Name = ReplacedText
   End If
Next

' echo the job is completed
WScript.Echo "Completed!"

tags

regular expression expressions regex regexp
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This page was cached on 16 Nov 2022 - 06:14.