Renaming files with RegEx - VisualCron - Forum

Community forum

K Craig
I need to rename files in a folder that are named as such:

I need to remove the date/time part of the filename and leave only the base name as this:

I would like to rename all files in a folder at one time, leaving the base file name and the correct file extension.

I'm not well-versed in regular expressions, and I need to accomplish this in a timeframe that prevents me from spending a lot of time learning regex.
any help would be much appreciated.
If there is always an underscore in the name, this should work:


The formula:



{NEWNAME()} is the current name of the file, eg MyFile_202002170900.txt

The first part of the formula: {STRING(Substring|{NEWNAME()}|0|{STRING(IndexOf|{NEWNAME()}|_)})}
This finds the position of the character_, and takes the substring up to the underscore. So that would be MyFile

The last part of the formula: {PATH(GetExtension|{TASK(Active|SourceFolder)}\{NEWNAME()})}
This finds the file extension, eg .txt

The result is the concatenation of those two, MyFile.txt
Match the underscore and everything up to and including the dot, replace with a dot:


Match an underscore followed by 12 digits and replace with NULL:

you could also break down the parts of the string into different capture groups that are surrounded by parenthesis, and replace the string with the capture groups (labeled from 1 left to right). Here group 1 is the group of characters after the start of the line but before the underscore, group 2 is the date, group 3 is the dot and extension. This way is useful for breaking the filename into it's constituent parts for other uses.



All methods have their pros and cons depending on the situation. Information is power!
yep, the first regex looks nicer than mine. I would go for that
By the way, K Craig wanted to rename all the files at once. In the rename file/Location tab, check "Is regex" and enter .*_\d{12}\.(txt|asc) in the include file mask to match files ending with an underscore followed by 12 digits and with an extension of txt or asc. Then in the rename settings tab, use this for the post process mask:
which replaces the underscore and 12 digits with NULL.

You may want to run a powershell script to do that. Here's one way should you want to give it a try based on this article: 

Create a job with a job variable that holds the path where the files live. I call it JobWorkingDir. Then create a powershell task where you have 1 parameter called SourcePath that is set to the job variable.
The script uses Get-ChildItem to list all the files in SourcePath. Then Where-Object filters the list to include only the files that match the regex of files that match starting with anything then ending in an underscore followed by 12 digits then a literal dot then the extensions txt or asc (as stated in the example. Tweak for your needs). Then that filtered list is passed to Rename-Item where the underscore and 12 digits are replaced with NULL, effectively deleting them.

[string] $SourcePath

Get-ChildItem -Path $SourcePath | Where-Object -FilterScript {$_.Name -match ".*_\d{12}\.[txt|asc]"} |
Rename-Item -NewName { $ -replace '_\d{12}', ''}

To make it even more versatile, make parameters for what to filter on and match/replace as well.
Scroll to Top