Renaming files with RegEx - VisualCron - Forum

Community forum

K Craig
2020-02-17T16:39:26Z
I need to rename files in a folder that are named as such:
MyFile_202002170900.txt
ThisFile_202002170901.txt
AFile_202002171001.asc

I need to remove the date/time part of the filename and leave only the base name as this:
MyFile.txt
ThisFile.txt
AFile.asc

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.
thomas
2020-02-17T20:27:47Z
If there is always an underscore in the name, this should work:

Capture.PNG

The formula:

{STRING(Substring|{NEWNAME()}|0|{STRING(IndexOf|{NEWNAME()}|_)})}{PATH(GetExtension|{TASK(Active|SourceFolder)}\{NEWNAME()})}

Explanation:

{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
Gary_W
2020-02-17T22:03:16Z
Match the underscore and everything up to and including the dot, replace with a dot:

{REGEX(Replace|MyFile_202002170900.txt|_.*\.|.)}
OR

Match an underscore followed by 12 digits and replace with NULL:
{REGEX(Replace|MyFile_202002170900.txt|_\d{12}|)}
OR

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.

{REGEX(Replace|MyFile_202002170900.txt|^(.*)_(.*)(\..*)$|$1$3)}

MyFile.txt

All methods have their pros and cons depending on the situation. Information is power!
thomas
2020-02-18T08:56:05Z
yep, the first regex looks nicer than mine. I would go for that
Gary_W
2020-02-18T21:05:45Z
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:
{REGEX(Replace|{NEWNAME()}|_\d{12}|)}
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: https://devblogs.microso...to-rename-files-in-bulk/ 

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.

Param(
[Parameter(Mandatory=$true)]
[string] $SourcePath
)

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

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