Community forum

Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


jprice1485
2014-05-06T16:04:26Z
I'm having a heck of a time figuring out how to strip unneeded data from a filename when renaming files.

Example filename:
_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF

I want to rename the file as:
_304853637_.pdf

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

Any ideas?

Jeff
Sponsor
Forum information
Support
2014-05-06T19:29:34Z
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.
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
bbusse
2014-05-14T20:35:21Z
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.

{STRING(Left|{STRING(Substring|{USERVAR(FileNameTest)}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|{MATH(Subtract|Integer|{STRING(Length|{USERVAR(FileNameTest)})}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|)})}|{STRING(IndexOf|{STRING(Substring|{USERVAR(FileNameTest)}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|{MATH(Subtract|Integer|{STRING(Length|{USERVAR(FileNameTest)})}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|)})}|_)})}


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.


_{STRING(Left|{STRING(Substring|{USERVAR(FileNameTest)}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|{MATH(Subtract|Integer|{STRING(Length|{USERVAR(FileNameTest)})}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|)})}|{STRING(IndexOf|{STRING(Substring|{USERVAR(FileNameTest)}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|{MATH(Subtract|Integer|{STRING(Length|{USERVAR(FileNameTest)})}|{STRING(IndexOf|{USERVAR(FileNameTest)}|{REGEX(Match|{USERVAR(FileNameTest)}|[0-9])})}|)})}|_)})}_.pdf


This results in this string as the value:
_304853637_.pdf
bbusse
2014-05-14T20:39:35Z
Examples
bbusse attached the following image(s):
KJDavie
2014-05-15T03:08:46Z
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)})}

Yields:

_304853637_.PDF

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:

{REGEX(MatchGetGroup|{TRIGGER(d9a226ae-0ad2-42fb-b518-1efd5af13bf7|LastTrigger|File.Result.Name)}|(.*)-(.*)-(.*)-(.*)-(.*)|1)}-
{REGEX(MatchGetGroup|{TRIGGER(d9a226ae-0ad2-42fb-b518-1efd5af13bf7|LastTrigger|File.Result.Name)}|(.*)-(.*)-(.*)-(.*)-(.*)|2)}-
{REGEX(MatchGetGroup|{TRIGGER(d9a226ae-0ad2-42fb-b518-1efd5af13bf7|LastTrigger|File.Result.Name)}|(.*)-(.*)-(.*)-(.*)-(.*)|3)}-
{REGEX(MatchGetGroup|{TRIGGER(d9a226ae-0ad2-42fb-b518-1efd5af13bf7|LastTrigger|File.Result.Name)}|(.*)-(.*)-(.*)-(.*)-(.*)|5)}

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.

http://www.visualcron.co...sts&t=3437#post16201 

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 : http://regexpal.com/ 
<I am not in anyway affiliated with this site, just a user !>

Cheers

Kevin
bbusse
2014-05-15T12:09:07Z
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 🙂

ErikC
2014-05-16T06:01:43Z
Hi,

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.

Regards,
Erik
Uses Visualcron since 2006.
thomas
2014-05-16T08:14:51Z
Hi

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

Thomas
Gary_W
2017-11-24T20:07:13Z
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.
{REGEX(MatchGetGroup|_xxxxxxxxx_yyyyyyyy_304853637_94245342__2014-05-04.PDF|_\d{1,}?_|0)}
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.

success!
Scroll to Top