Evaluators - Need tip to migrate existing JScript

Hi!

Currently using this JS code to populate a column's content using old style scripting.

//extract date from filename
var date = String(scriptColData.item.name_stem).match(/\d{4}-\d{2}-\d{2}/);
//extract years of warranty from filename
var warranty = String(scriptColData.item.name_stem).match(/\[\d+.*\]/);

How would one do this with an evaluator code?

Just tried:

date = RegExS(name,"\d{4}-\d{2}-\d{2} (.*)");
Output(date);

But this obviously returns True or False if a filename fits the conditions or not.

I would like to extract the "date" from a filename in var date and the last part of the filename in variable warranty, eg.
2022-03-04 TeVi Severin Microwelle [2 Jahre Garantie].pdf

name = "2022-03-04 TeVi Severin Microwelle [2 Jahre Garantie].pdf";
date = RegExS(name,"(^\d{4}-\d{2}-\d{2}) (.*)", "\1");
rest = RegExS(name,"(^\d{4}-\d{2}-\d{2}) (.*)", "\2");
Output(date);
Output(rest);

If the date might appear in the middle of the name, take out the ^ at the start of both patterns. (But you'll only get the text after the date in rest, and lose any text before the date.)

I did a small edit to RegExS [Directory Opus Manual] to include an example and state which replacement syntax it uses (since some things use \1 and others use $1).

1 Like

I was near the solution, 'cause my other coding was like this, without the round brackets

date = RegExS(name,"\d{4}-\d{2}-\d{2} (.*)", "\1");
Output(date);

Thanks Leo for this prompt answer and help!

1 Like

May I come back and ask for another tip regarding RegExS function?

Continuing the example above, I would need the last part of the filename starting from

[2 Jahre Garantie].pdf

When using

name = "2022-03-04 TeVi Severin Microwelle [2 Jahre Garantie].pdf";
warranty = RegExS(name,"(\[\d+)", "\1");
Output(warranty);

I get

2022-03-04 TeVi Severin Microwelle [2 Jahre Garantie].pdf
--> (empty result)

I expected [2 as a result in variable warranty, please see: regex101: build, test, and debug regex

Where is my fault?

Edit: Just found my error: It's all about grouping :wink:

name = "2022-03-04 TeVi Severin Microwelle [2 Jahre Garantie].pdf";
warranty = RegExS(name,"(.*)(\[)(\d+)(.*)", "\3");
Output(warranty);

Gives me just the digits, in my case: 2

I'm not sure why it's needed in this case (need to look in more detail), but this seems to work:

warranty = RegExS(name, ".*(\[\d+).*", "\1");
1 Like

Thanks Leo, see my edit in my previous posting :point_up_2:

1 Like

Try RegEx instead of RegExS.

1 Like

Continuing my JScript conversion, I hit a new challenge: date calculations!

Given this evaluator script

expiration = DatePart(Now(),"YYYY-MM-dd");
output(expiration);

results in 2023-12-29. Now I want to add e.g. 2 years to this date.

While this was pretty easy before, using a JScript

var warranty_yrs = 2;
var df = DOpus.Create.Date(date[0]);
df.Add(warranty_yrs,"y");

it occurs to be a somehow bigger challenge with Evaluators :grimacing:

Further tests showed that this might be one solution:

expiration = DatePart(Now(),"YYYY-MM-dd") as date;
expiration = (expiration + 2 * 365 * 24 * 60 * 60) as date;
output(expiration);

At least I almost get the desired date: 2025-12-28.

I did a quick test, using parts of my old JScript and the result is however 2025-12-29.
So, JScript date objects respect leap years like 2024, the manual date calculation above, does not of course. For some obvious reasons, I do not want to add leap year detection, you know :slightly_smiling_face:

Is there a better way, using Evaluator functions?

You can add two years to a date like this:

expiration = Now() + "2y";

Note that the YYYY code for the DatePart() function returns the ISO year, not the calendar year, which can differ around the end of the year.

1 Like
expiration = Now() + "2y";

Wow, did not test so far, however this should be part of the documentation !

Thanks @Jon!

Edit: Works perfectly, used like this in my context (simplified):

warranty_yrs = 2;
expires_date: date = Now();
expires_date = expires_date + Format("%1y", warranty_yrs);

Did not found an easier solution so far for a dynamic date calculation other than using the Format function.