MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   The JavaScript Library (http://forums.pti.com/forumdisplay.php?f=23)
-   -   Reg Ex or Javascript for File Name (http://forums.pti.com/showthread.php?t=10670)

ReminderVDP July 6th, 2022 11:35 AM

Reg Ex or Javascript for File Name
 
I've got a rule in a file that looks for a PDF on a server in a folder based on the file name. It's looking for the file based on a field in my data but I'm adding two zeros in the rule because the data field does not contain the zeros.

For example, the data field has "9" in it. The file it is looking for has "009" in it. My current rule is this:
Code:

var issueNumber = 'GH00' + Field("MagIDShort");

var pathName = "X:\\EDITORIAL\\FinalGutsforProofs\\GHM\\";
var FullResourcePath = pathName + issueNumber + "\\" + 'GH_magazine-00' + Field("MagIDShort") + ".pdf"; //change to match your data file field
var x = new FusionProResource(FullResourcePath, "graphic", 1);
if (!x.exists)
ReportError("Graphic not found: " + FullResourcePath);
var pdfString = '';
var pages = (x.countPages);
for (var pgnbr = 1; pgnbr <= pages; pgnbr++)
{
x.pagenumber = pgnbr;
pdfString += x.value + '<p>\n';
}
Print("Result is: " + pdfString);
return pdfString;

It adds the two zeros in line 4. However, we are coming up on issue 10 so I will only need to add one zero to the rule, but I still have people making files looking for issue 9 and still need the two zeros in the file name.

Is there some reg ex that would look for one or two zeros? I'm not good at reg ex so I'm looking for a solution that can be used for either file name, 009 or 010. Thanks in advance.

ThomasLewis July 6th, 2022 03:36 PM

Re: Reg Ex or Javascript for File Name
 
I think what you are looking for is the FormatNumber function
FormatNumber("000", inputNumber)

ReminderVDP July 6th, 2022 03:46 PM

Re: Reg Ex or Javascript for File Name
 
Thomas,
Would that go in place of the 00 in the code below?

Code:

+ 'GH_magazine-00'

ThomasLewis July 6th, 2022 04:12 PM

Re: Reg Ex or Javascript for File Name
 
Like this if I'm reading your code right:

Code:

... "\\" + 'GH_magazine-' + FormatNumber("000", Field("MagIDShort")) + ".pdf";

ReminderVDP July 7th, 2022 07:53 AM

Re: Reg Ex or Javascript for File Name
 
That's what I thought you meant. I will try that. Thanks!

Dan Korn July 7th, 2022 11:43 AM

Re: Reg Ex or Javascript for File Name
 
If you want to test for multiple possibilities, you can do that in a loop, something like this:
Code:

var x;
for (var zeros = 0; zeros <= 3; zeros++)
{
    var issueNumPadded = new Array((zeros || 0) + 1).join("0") + issueNumber;
    var FullResourcePath = pathName + issueNumber + "\\" + 'GH_magazine-' + issueNumPadded + ".pdf";
    x = new FusionProResource(FullResourcePath, "graphic", 1);
    if (x.exists)
        break;
}
if (!x || !x.exists)
    ReportError("Graphic not found for " + issueNumber);


ReminderVDP July 20th, 2022 02:31 PM

Re: Reg Ex or Javascript for File Name
 
Thanks Dan. I did get it to work with the code ThomasLewis put in here. That was a huge help.


All times are GMT -7. The time now is 08:18 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.