Jump to content

Output file name from input file name


JeremyT

Recommended Posts

I am working on a job where I'd like the output pdf to have characters from the input file name.

 

Input file name: ORDERS_01172014_073000_DOOR.xls

 

I'd like the final pdf to be named: FORM-01172014_073000.pdf

 

I've done something similar on this post with code that Dan suggested:

http://forums.pti.com/showpost.php?p=14231&postcount=10

 

This code was used in a OnRecordStart.

 

if  (FusionPro.Composition.processedRecordNumber == 1 ||  (FieldChanged("Form Type") && Field("Form Type")=="Form F")) {
   var outputPrefix = Field("Form Type")=="Form F" ? "Form F" : "Combined";
    var outputName =  GetFileName(FusionPro.Composition.inputFileName).replace(/^[^1-9]*([^\.]*)20(\d{2})\.\w+$/,outputPrefix+'-$1$2.');
   var outputPath = "/some path here/folder/etc/"; // or base this on a field value, or whatever
   FusionPro.Composition.OpenNewOutputFile(outputPath + outputName + FusionPro.Composition.outputFormatExtension);
   Print("Changing to output file: " + outputName + FusionPro.Composition.outputFormatExtension);
}

This time is simpler in that I am only outputting 1 pdf. How do I adjust?

 

 

Thanks,

Jeremy

Link to comment
Share on other sites

I am working on a job where I'd like the output pdf to have characters from the input file name.

 

Input file name: ORDERS_01172014_073000_DOOR.xls

 

I'd like the final pdf to be named: FORM-01172014_073000.pdf

It's hard to know from just one data point. Will the input file always start with "ORDERS"? Will it always end with "DOOR.xls"? Can you show examples of other input file names, and the desired output file names, so that I can see the pattern?

Link to comment
Share on other sites

Input file most often starts with "ORDERS" and ends with "DOOR.xls". Only 1 or 2 a month that don't.

 

But, I also have a Spanish version of this job that is run a couple of days each month with input files like:

ORDERS_11042013_073000_DOOR SP.txt

 

Is there a way to capture just the numerals with the underscore between so that I could use the same code for both versions(English and Spanish)?

 

Screen shots of the input and output files are attached.

ScreenShot2014-01-22at11_39_22AM.jpg.a1aa7402dba855ed8b05aa7897cb0201.jpg

ScreenShot2014-01-22at11_40_26AM.jpg.398e2dcbef03a75e9f3ab99fdb5a4bca.jpg

Link to comment
Share on other sites

Try this:

var outputName = "FORM" + GetFileName(FusionPro.Composition.inputFileName).replace(/[^\d_]/g, '') +
               "." + FusionPro.Composition.outputFormatExtension;

And after that, you can just call FusionPro.Composition.OpenNewOutputFile with that file name, or prepend a path to it first if you want.

Link to comment
Share on other sites

  • 2 weeks later...

Can the underscores before and after the numerals be eliminated?

 

Right now it produces:

 

FORM_01172014_073000_.pdf

 

I'd like it to be:

 

FORM-01172014_073000.pdf

 

I know how to add the hyphen, but I am unsure how to remove the underscores.

Link to comment
Share on other sites

var outputName = "FORM-" + GetFileName(FusionPro.Composition.inputFileName).match(/\d+_\d+/g)[0] +
               "." + FusionPro.Composition.outputFormatExtension;

 

I get the error "OnJobStart, line 1:TypeError: GetFileName(FusionPro.Composition.inputFileName).match(/\d+_\d+/g) has no properties" using the code above.

Link to comment
Share on other sites

It appears you put a space between the "m" and "atch"

 

I copied the code from this forum page.

 

For some reason, when I typed the error code my response had the extra space in it.

 

The code in my pdf does not have the extra space in it.

Link to comment
Share on other sites

I get the error "OnJobStart, line 1:TypeError: GetFileName(FusionPro.Composition.inputFileName).match(/\d+_\d+/g) has no properties" using the code above.

JavaScript's String.match function can return null if there's no match found, so you should write your code to be able to handle that condition.

 

Also, you'll definitely fail to get a match at rule validation time, in the Rule Editor, because FusionPro.Composition.inputFileName represents an empty string in that context. Like the rest of the properties of the FusionPro.Composition object, it's valid only at composition time.

 

So it should work when you compose, as long as the input file name conforms to the pattern you specified in the original post, i.e. it matches the regular expression pattern /\d+_\d+/ (one or more digits, followed by an underscore, followed by one or more digits).

 

If you want to avoid the error, both in the case where the file name does not follow the convention you're expecting, and for rule validation, you can do something like this:

var theMatch = GetFileName(FusionPro.Composition.inputFileName).match(/\d+_\d+/g);
if (!theMatch)
{
   Print("Input file name does not match the expected format."); 
}
else
{
   // call FusionPro.Composition.OpenNewOutputFile etc... 
}

Or, just use String.replace instead, as step suggests in his most recent post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...