Skip to main content
Participant
March 21, 2019
Question

"Export Layers as Files" with the original file name as prefix

  • March 21, 2019
  • 1 reply
  • 899 views

Hello,

I'm currently working on an action which will be implemented in batch processing and utilizes "export layers to files" as saving method. The ultimate goal is to create three new versions of every image in a folder.

Therefore the folder before the actions looks like this:

a.jpg
b.jpg
c.jpg

I want this kind of output:


a.jpg
a_0000_version1.jpg
a_0000_version2.jpg
a_0000_version3.jpg

b.jpg

b_0000_version1.jpg

b_0000_version2.jpg

b_0000_version3.jpg

c.jpg

c_0000_version1.jpg

c_0000_version2.jpg

c_0000_version3.jpg


So I have the original file + all three new versions in the folder.
My current problem is that the dialog for "Export Layers To Files" asks for a prefix which is defined after the current file name when recording the action.


So my current output looks like this:

a.jpg
a_0000_version1.jpg
a_0000_version2.jpg
a_0000_version3.jpg

b.jpg

c.jpg

Basically the batch processing overwrites the first set of versions over and over again because it recorded the first file name.
Is there a way to set the "Layer to Files"-prefix to the current processed file name?

Thank you in advance!

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
March 21, 2019

It sound like you need to add logic into the file name routine to make it like the Image Processor scripts.  They will not overwrite files.  If the output file exist the will add a suffix sequence number that is unique. the function in Image Processor script look like this. Copy export layer to file to a new script  name and see If you can add that type of code and use the modified script to do your  work.

///////////////////////////////////////////////////////////////////////////////

// Function: CreateUniqueFileName

// Usage: Given a folder, filename, and extension, come up with a unique file name

// using a numbering system

// Input: string for folder, fileName, and extension, extension contains the "."

// Return: string for the full path to the unique file

///////////////////////////////////////////////////////////////////////////////

function CreateUniqueFileName( inFolder, inFileName, inExtension ) {

   inFileName = inFileName.replace(/[:\/\\*\?\"\<\>\|]/g, "_");  // '/\:*?"<>|' -> '_'

   var uniqueFileName = inFolder + inFileName + inExtension;

   var fileNumber = 1;

   while ( File( uniqueFileName ).exists ) {

      uniqueFileName = inFolder + inFileName + "_" + fileNumber + inExtension;

      fileNumber++;

      }

   return uniqueFileName;

}

JJMack