Rename Files - Preserve only a portion of it - VisualCron - Forum

Community forum

I'm having a heck of a time figuring out how to strip unneeded data from a filename when renaming files.

Example filename:

I want to rename the file as:

The "xxxxxxxxx" and "yyyyyyyy" elements above are variable in length but are always alpha characters.

Any ideas?

I think it is hard without adding more logic to it, like using .NET code Task or create a script. The problem for "simple" Variable renaming is that you need some kind of fixed length or other more common part of the string.
Please like  VisualCron on facebook!
I got bored.... So... I did some assuming... And keep in mind this is VERY hard to read, but it'll get exactly the number you were looking for (truncates anything that is before the first numeric value in the string, in your example that'd be '3'. Then, truncates anything AFTER (and including) the first underscore that's left.

For doing this, I created a variable called FileNameTest with the string of "_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF" as it's value.

That equates to: {USERVAR(FileNameTest)}

I then used various REGEX variables to get the result I wanted. You said the xxx and yyy was always alpha (not Alphanumeric)... so that's why i look for the first numeric character and trim everything before it.


One problem, It works in VisualCron 6.2.2 but not in 7.1.4 or the new 7.1.7 (beta). There's some variable parsing error that tosses it into an Infinite Loop. In 6.2.2 it returns the value as: "304853637"
In 7.1.4 and 7.1.7, I receive this error with alot of text:
TranslateVariables->Infinite loop error on string

In the code/variable example above, replace every instance of the
"{USERVAR(FileNameTest)}" with your actual variable or actual filename text.

So... if you always have alpha or non numeric characters up front... and then everything after the first underscore following the alpha can go away... That'll be your file name. Then you just have to add the _ to the beginning and end along with a file extension.


This results in this string as the value:
bbusse attached the following image(s):
Not as Robust as Brian's solution but we have a lot of this, from banks in particular where the filename is a complex, multi-part filename with a known delimiter.

We do a fair bit of this with REGEX Match Get Group.

i.e. your example:

xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF (Current Filename variable set to this)

_{REGEX(MatchGetGroup|{USERVAR(Current Filename)}|(.*)_(.*)_(.*)_(.*)_(.*)_(.*)|3)}_{PATH(GetExtension|{USERVAR(Current Filename)})}



Yes the delimiter is hard coded in the set variable . . . . but it is in the file (& regex) anyway. . . .

And further we sometimes have just one section we want to lose so we end up with this sort of thing:


Section 4 being Problematic above and being excluded.

Have done so in other REGEX examples on the VisualCron forum but we *always* run complex regex through a set variable task and then use the resulting variable as a self documenting way to show in the logs what happened. 

Historically we kicked out to Powershell for a lot of REGEX and other complexity, but thankfully this is happening less and less with VC

You may find this helpful in your travels : 
<I am not in anyway affiliated with this site, just a user !>


Nice. I like your way better. I don't do a lot of regex, but I can muddle my way through it and usually get the results I'm wanting. Yours is cleaner 🙂


Using a .NET task it is very easy:

 public static string CreateNewFilename(string oldname)
 	//split on the '_' character
 	string[] parts = oldname.Split('_');

 	//create the new filename
 	return "_"+parts[3]+"_.pdf";

Just pass in the current filename and it will return the new name. This script doesn't have error checks, you might want to add that part.
Use the output in the file rename task as the new name, that's it.

Uses Visualcron since 2006.

This solution is a bit silly, but I'll just mention it anyway. If the format is always the same (except length varies), you could use the 'Get column by row, column and column separator' function.
Split the filename using '_' as separator.

So {STRING(GetColumn|1|4|_|_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF)} results in 304853637

{STRING(GetColumn|1|5|_|_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF)} results in 94245342

and then you can combine the parts

I just stumbled on this post and figured I'd throw in my 2 cents for future searchers, just to confuse things, I mean show yet another way to approach the problem with a regex. I'm making an assumption that the 3rd grouping that you want is always a string of digits.
Returns the first occurrence of a string of at least 1 digit surrounded by underscores inclusive. "_304853637_"

So {REGEX(MatchGetGroup|_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF|_\d{1,}?_|0)}.pdf
should do the trick.

Scroll to Top