Skip to main content
Participant
June 5, 2019
Question

Rename based on If/Then/Else string Criteria

  • June 5, 2019
  • 2 replies
  • 418 views

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.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
June 5, 2019

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.

Participant
June 6, 2019

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.

try67
Community Expert
Community Expert
June 6, 2019

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.

Legend
June 5, 2019

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.