RegEx problem compare names

Post Reply
congomonster
Member
Posts: 25
Joined: Wed Jul 03, 2013 5:40 pm
Location: Germany
Contact:

RegEx problem compare names

Post by congomonster » Thu Jan 13, 2022 5:29 pm

Hi,

i have another questions. i hope someone can help me out...
I want to check in the destination folder if a file exist. If the file is there, then the actual job should be send too.
But i have a problem with the name part. The actual file has the "1_Somename__925171_Di_6-AM-42.pdf" and the second one has the name:
"1_SomeOtherName__925171_Di_6-AM-43.pdf".

I have the job name and the name from the second file. But when it comes to comparsion the regex is not working.
Or i doing something wrong :)

First i get the name of the job:

Code: Select all

const jobName = await job.getName(true);
Then i split the name to cut out the "Somename" part.

Code: Select all

const jobNameSplit = jobName.split('_');
Then i get the "SomeOtherName" Part from the SwitchScripter. ( I thougt it would be cool if this name get changed in the future)

Code: Select all

const pdfComparsionName = await flowElement.getPropertyStringValue("pdfComparsionName");
Then there is my RegEx code:

Code: Select all

const theregex = / [\w-.] /;
After that i put together the name for the second file. The one i searching for

Code: Select all

const pdfComparsionNameSearch = jobNameSplit[0] + '_' + pdfComparsionName + '__' + jobNameSplit[2] + '__';
And i have the path also in a const.

After that i do:

Code: Select all

try {
    if (fs.existsSync(pdfComparsionFilePath + pdfComparsionNameSearch + theregex)) {
      await job.log(LogLevel.Info, 'The file is here! hooray!');
      //await job.sendToSingle();
    } else {
      await job.log(LogLevel.Info, 'There is no file.');
    };
  } catch (err) {
    await job.log(LogLevel.Warning, 'Something went wrong');
  };
So i think the last part with the regex is the problem. If i write the filename without the regex and with the filename hardcoded it works perfect.
So i hope this is not too much. :D

User avatar
JimmyHartington
Member
Posts: 93
Joined: Tue Mar 22, 2011 7:38 am

Re: RegEx problem compare names

Post by JimmyHartington » Fri Jan 14, 2022 8:32 am

I have not done any of the node.js scripting in Switch yet.
But done a little in the "old" way.

Could you try to log the filename you create and see if the filename is invalid for the filesystem?
Or try to use the name you log and hardcode that to see if that is the problem?

freddyp
Advanced member
Posts: 691
Joined: Thu Feb 09, 2012 3:53 pm

Re: RegEx problem compare names

Post by freddyp » Fri Jan 14, 2022 9:20 am

From your code I have the impression that you think that fs.existsSync will evaluate the regex as part of the string as a kind of wildcard in order to check if the file exists, but that is not how it works. Also, concatenating a regex to a string will add the regex to the string as a string. That is certainly not what you want.

What you have to do is to get a list of all the files in that folder (fs.readdirSync) and loop over the array trying to match each filename with the regex.

I certainly do not want to discourage you from writing a script, but this app will do what you need with a lot more bells and whistles:https://www.enfocus.com/en/appstore/pro ... -hierarchy

congomonster
Member
Posts: 25
Joined: Wed Jul 03, 2013 5:40 pm
Location: Germany
Contact:

Re: RegEx problem compare names

Post by congomonster » Fri Jan 14, 2022 2:00 pm

Hi,

first of all thank you for your answers!
As freddyp guessed, that is exactly how i thought this will work. ;)
Thanks for the hint that i have to watch the folder.

I think that i have to rewatch await and async in Javascript. Sometimes i don't understand when i have to use what.
For example, this works fine only in NodeJS:

Code: Select all

const fs = require('fs');

const path = "./path to folder";

fs.readdir(path, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});
It shows all the files in the console log. If i use this witch switch i get an error. I read that readdir is the async variation of readdirSync. So I'm a little bit confused what i can use now. I wrote something like this:

Code: Select all

const fs = require('fs');

const path = "./path to folder";

fs.readdir(path, (err, files) => {
  files.forEach(file => {
     await job.log(LogLevel.Info, 'Some Text');
  });
});

congomonster
Member
Posts: 25
Joined: Wed Jul 03, 2013 5:40 pm
Location: Germany
Contact:

Re: RegEx problem compare names

Post by congomonster » Fri Jan 14, 2022 2:35 pm

Okay, i have something that works now. I downloaded some of the example scripts provided by enfocus.

Code: Select all

let pdfComparsionFilePathFiles;
  try {
    pdfComparsionFilePathFiles = await fs.readdirSync(pdfComparsionFilePath);
    await pdfComparsionFilePathFiles.forEach(file => {
      job.log(LogLevel.Info, 'The file name is: %1', [file]);
    });
  } catch (error) {
    //if the content of the folder cannot be read the element cannot function
    await flowElement.failProcess("Could not reach the folder");
    return;
  };
With this all the files in the output folder get logged on the LogLevel.Info

Post Reply