RegEx Rename job

Post Reply
jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

RegEx Rename job

Post by jdold » Thu Mar 14, 2019 9:30 pm

I could use a little help with an email flow.

Trying to watch an email and create folders on a server named the email subject-email sender-email date. and download all attachment into them.
Also, unzip any zipped files and put original zip in original zip folder inside that hierarchy.
I can do that but the problem comes in when the subject has special characters,
Then it will not create a folder and become a problem job.
Found some help in this post- viewtopic.php?f=13&t=2214&p=7657&hilit= ... ject#p7657
I put that script in my rename job under - replace by- regexpression defined.
That will remove those characters,
but the problem is now, have lost other email info and cannot name folders as I originally wished.
Another issue is now when I dismantle the job to get the zip file and set hierarchy, and then Archive hierarchy I get a folder for every piece in original job.

Too much to wish for?

mclarke
Member
Posts: 41
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: RegEx Rename job

Post by mclarke » Thu Mar 21, 2019 2:55 pm

Try setting the data you need to private data items as soon as the job comes in. Run the script on just the private data item that contains the special characters. The rest should not be touched.

With the most recent version of Switch. there is an option in the folder properties to set private data items, so you don't need a special script for setting them. The syntax is

key=value (one key per line). The value can be a job variable such as the email address, subject, etc...

In your script doing the replacement, you will now reference the private data key and change it.

Also, your Rename job element should reference a script expression, not a regex. You do the work in the script expression and return the result as the new name.

jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

Re: RegEx Rename job

Post by jdold » Mon Mar 25, 2019 2:38 pm

Thank you for the response.
This is fairly new to me and I am not quite getting it.
Could you look at my screenshots and tell me exactly where I am going wrong?
The attachment set private date.png is no longer available
The attachment select private data.png is no longer available
set private date.png
set private date.png (97.41 KiB) Viewed 523 times

jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

Re: RegEx Rename job

Post by jdold » Mon Mar 25, 2019 2:40 pm

set private date.png
set private date.png (97.41 KiB) Viewed 521 times

jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

Re: RegEx Rename job

Post by jdold » Mon Mar 25, 2019 2:41 pm

script change name.png
script change name.png (121.94 KiB) Viewed 520 times

mclarke
Member
Posts: 41
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: RegEx Rename job

Post by mclarke » Mon Mar 25, 2019 3:05 pm

I feel your pain. I am also trying to learn Javascript as I go in creating more advanced flows. I find myself scouring the Switch 2018 scripting help file and also looking online and in these forums for various answers. It can seem very overwhelming, especially since there is not much available help from Enfocus (webinars, how-to videos) on the scripting process for beginners.

From your screenshots, it looks like you are setting the private data correctly, but not calling the right value in your script expression. Do you have the Scripting module? It makes working on script expressions and full scripts much easier, because it comes with the SwitchScripter writing tool. You can create a "test environment" to try out your script and see if it gives you the correct result.

Anyway, I digress. The value you need to call is job.getPrivateData(Foldername). Also, you will need to pass back the value in your variable "result" at the end of the script. ALso, I am not too sure about your regExp, I think some more of those characters need to be escaped.

var input = job.getPrivateData(Foldername);
var regExp = /[^(\"?\'!.*:<>)]/g;
var result = input.match(regExp);
result.join("");

result

jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

Re: RegEx Rename job

Post by jdold » Mon Mar 25, 2019 3:16 pm

argh
one of my screen shots did not post right-
This was supposed to be in there also.
Am i selecting the private data right?
select private data.png
select private data.png (162.39 KiB) Viewed 518 times

mclarke
Member
Posts: 41
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: RegEx Rename job

Post by mclarke » Mon Mar 25, 2019 4:06 pm

It's really much easier to see if you are getting correct results by having a "live" test job in the flow that has actual data attached to it. Let's go back to what you are trying to accomplish. From your original post, you want to create a folder that is named after some incoming email data, and store any attached files in that so-named folder.

The set of data you selected were: Email.Subject, Email.Subject, Email.Date. So, now you have to get your folder named. You need to pass back from the Rename Job element the folder name. How to best go about it? Well, what I would try and do with limited working knowledge of how scripting works is use the Rename Job element to do the building of the whole name. A script expression can be used to work on the subject data, then use Add Suffix in a following couple of actions to add the rest of your data.

Within the script expression,the Email.Subject cannot be called directly (at least, I couldn't find a reference in the scripting guide), so you will need to set that information to private data in order to call it in the script expression. Once your script expression is working correctly and passing back the data you expect, then just add the last two pieces by calling the data from the Email information held in the job.

You should test your regex statement in an online regex tool before entering the pattern in the script expression. That will make sure it's doing what you expect it to do. You should also check what the Email.Date variable is going to contain; if the data is formatted with slashes "03/25/19", that's going to be a no-no for the folder name as well.

jdold
Newbie
Posts: 6
Joined: Tue Feb 26, 2019 6:43 pm

Re: RegEx Rename job

Post by jdold » Wed Mar 27, 2019 4:57 pm

I am running tests on all this now.
I still have not gotten it, but will let you know if I do.
Thank you very much for the info.

mclarke
Member
Posts: 41
Joined: Thu Feb 28, 2013 5:29 pm
Location: Syracuse, NY

Re: RegEx Rename job

Post by mclarke » Wed Mar 27, 2019 5:50 pm

You're very welcome. Those of us new to scripting need to stick together. :)

Post Reply