Rename based on If/Then/Else string Criteria

New Here ,
Jun 05, 2019

Copy link to clipboard

Copied

I grabbed some code on how to rename a file based on the string  position within the file. That doesn't work for me because the data in the files changes so the text I need for renaming the file can be shifted. What I need instead is some sort of If/Then/Else formula.

I found that code here: https://forums.adobe.com/thread/2364649 but I'm trying to tweak it to suit my needs. I am incorporating this script into an Action in Acrobat that will run through a batch and rename (or create new files) in a designated folder.

This is the criteria I need for the rename:

IF the PDF contains the text "Packing List" THEN I need the new file name compiled by returning the value AFTER the string "Reference to Invoice" + "." + the value AFTER the string "Shipment Number" + "." + "pl" + ".pdf"

This would look something like this: 123456789.PPHB12345.pl.pdf

ELSE

I need the new file name compiled by returning the value AFTER the string ""Customs Invoice" + "." + the value AFTER the string "Shipment No." + "inv" +.pdf"

This would look something like this: 123456789.PPHB12345.inv.pdf

I'm new to javascript so need some assistance in how to write the code to do this.

var docRef = app.activeDocument;

var searchString = "Packing List";

var pronmbr = getPageNthWord(0,28,true) + "." + getPageNthWord(0,54,true)  -this was part of of the original code from above and doesn't apply here.

var re = /\.pdf$/;

var fname = this.documentFileName.replace(re,"_");

var filename =  pronmbr + "INV" +  ".pdf";

Thank you for your help.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

99

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Rename based on If/Then/Else string Criteria

New Here ,
Jun 05, 2019

Copy link to clipboard

Copied

I grabbed some code on how to rename a file based on the string  position within the file. That doesn't work for me because the data in the files changes so the text I need for renaming the file can be shifted. What I need instead is some sort of If/Then/Else formula.

I found that code here: https://forums.adobe.com/thread/2364649 but I'm trying to tweak it to suit my needs. I am incorporating this script into an Action in Acrobat that will run through a batch and rename (or create new files) in a designated folder.

This is the criteria I need for the rename:

IF the PDF contains the text "Packing List" THEN I need the new file name compiled by returning the value AFTER the string "Reference to Invoice" + "." + the value AFTER the string "Shipment Number" + "." + "pl" + ".pdf"

This would look something like this: 123456789.PPHB12345.pl.pdf

ELSE

I need the new file name compiled by returning the value AFTER the string ""Customs Invoice" + "." + the value AFTER the string "Shipment No." + "inv" +.pdf"

This would look something like this: 123456789.PPHB12345.inv.pdf

I'm new to javascript so need some assistance in how to write the code to do this.

var docRef = app.activeDocument;

var searchString = "Packing List";

var pronmbr = getPageNthWord(0,28,true) + "." + getPageNthWord(0,54,true)  -this was part of of the original code from above and doesn't apply here.

var re = /\.pdf$/;

var fname = this.documentFileName.replace(re,"_");

var filename =  pronmbr + "INV" +  ".pdf";

Thank you for your help.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

100

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 05, 2019 0
Most Valuable Participant ,
Jun 05, 2019

Copy link to clipboard

Copied

Yoy can only get the words one at a time. So for instance to get the value after Packing List you have to find the word Packing followed by the word List, not both at once. Then you get the word after List.  This is rather awkward to code, but share your attempts and we can help you get more skills towards this task.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Most Valuable Participant ,
Jun 05, 2019

Copy link to clipboard

Copied

Keep in mind you can't actually rename a file using a script, only save it under a new name, and even that requires a privileged context, like running the script from the Console, in an Action or from a folder-level script.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
New Here ,
Jun 06, 2019

Copy link to clipboard

Copied

Creating new files is fine. I understand that  I can't actually rename an existing file. I will specify the directory for the newly named files in the export within the code. I just need help in figuring out the coding sequence.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
Most Valuable Participant ,
Jun 06, 2019

Copy link to clipboard

Copied

This is a complicated task. As mentioned, you can only access the words in the file one at a time. So you will need to construct a set of if-conditions that handle that. Or you could just read the entire contents of the page to a string and then parse it as a whole.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
New Here ,
Jun 06, 2019

Copy link to clipboard

Copied

How does that syntax look? Can you give me an example to work from as far as how nested if-conditions would be written?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
try67 LATEST
Most Valuable Participant ,
Jun 06, 2019

Copy link to clipboard

Copied

if (word1=="Packing" && word2=="List") {

     // start searching the page again

     if (word3=="Reference" && word4=="to" && word5=="Invoice") {

          // save the following words to a string to be used later on

     }

     if (word3=="Shipment" && word4=="Number") {

          // save the following words to a string to be used later on

     }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0