Since Windows rename command doesn’t support regular expression, so today I made a VB Script to batch rename multiple files using regex method.
What is Regular Expression?
Basically, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are based. But we will not dig into that. Since most people including myself are lazy to type, you will usually find the name abbreviated to regex or regexp. I prefer regex, because it is easy to pronounce the plural “regexes”.
Click read more to see the script
The Script
' ----------------------------------
' Name : Regex Rename File Script
' Version : 1.0
' Author : Lucky
' ----------------------------------
WScript.Echo "Rename File Script v1.0 by Lucky"
WScript.Echo "================================"
WScript.Echo
Dim args
Set args = WScript.Arguments
Dim pattern
Dim replacement
If args.Count<>2 Then
WScript.Echo "Usage: cscript rename.vbs [searchpattern] [replacement]"
WScript.Quit()
Else
pattern=args(0)
replacement=args(1)
WScript.Echo "Replace """ & args(0) & """ to """ & args(1) & """:"
WScript.Echo
End IF
'Start rename
Dim fso,cf,regex
Dim oldname,newname
Dim matches
Set fso = CreateObject("Scripting.FileSystemObject")
Set cf = fso.GetFolder(".")
Set regex = New RegExp
regex.Pattern=pattern
regex.IgnoreCase=True
Set colFiles = cf.Files
For Each objFile in colFiles
oldname=objFile.Name
Set matches = regex.Execute(oldname)
If matches.Count>0 Then
newname=regex.Replace(oldname,replacement)
Wscript.Echo "Rename """ & oldname & """ to """ & newname & """"
objFile.Move newname
End If
Next
Usage
First you need to copy the script above and paste it on notepad, save it to “rename.vbs”. Regex rename script needs two parameters, pattern and replacement. The script will find matching filename with the pattern in current folder, and replace it to replacement text.
Usage: cscript rename.vbs [searchpattern] [replacement]
Examples
You can use a few examples with the script
#simply rename a file from image.jpg to image2.jpg
cscript rename.vbs image.jpg image2.jpg
#rename some file with wildcards, image?.jpg to picture?.jpg
cscript rename.vbs image(.).jpg picture$1.jpg
#another example with wildcards, rename file start with image to picture
cscript rename.vbs image(.*) picture$1
Limitation
Currently the latest VBScript version 5.5 regular expression engine has some limitation, which are:
- No \A or \Z anchors to match the start or end of the string. Use a caret or dollar instead
- Lookbehind is not supported at all. Lookahead is fully supported
- No atomic grouping or possessive quantifiers
- No Unicode support, except for matching single characters with \uFFFF
- No named capturing groups. Use numbered capturing groups instead
- No mode modifiers to set matching options within the regular expression
- No conditionals
- No regular expression comments. Describe your regular expression with VBScript apostrophe comments instead, outside the regular expression string
Source: Regular-Expression.info
No related posts.
