Skip to main content
Known Participant
August 4, 2019
Answered

Save jpeg replacing only part of file name

  • August 4, 2019
  • 3 replies
  • 3819 views

Hi - completely new to scripting, but I need help so that I can use this function to rename part of my file when batch processing my images.

For example:

I want to change from files named:    DSC_1234

TO

all files named:    (CUSTOMNAME)_1234

I hope it's clear what I am trying to do.

I don't need to rename any text within the file, only the file name itself

Thanks in advance for taking the time to help out a new scriptor!

This topic has been closed for replies.
Correct answer Stephen Marsh

Thank you!

Maybe I'm not understanding how to add the script into my action because the action is running but it is not following the script you have helped make for me.

This is what I have done, I am using NOTEPAD and saving as a .jsx file on Windows 10.   **and not as .jsx.txt only .jsx

If Notepad is the wrong program let me know.

The steps I follow are below:

I take your script, copy into notepad: -->

  1. var doc = app.activeDocument; 
  2. // regular expression capture group find/replace for one or more consecutive digits 
  3. var uniqueDigits = doc.name.replace(/(^.+_)(\d+)(\.[^\.]+$)/, '$2');  
  4. // change 'custom name_' & '.jpg' strings as required 
  5. var newName = 'custom name_' + uniqueDigits + '.jpg';  

Changed your line #5 to my own text: -->

   5. var newName = 'MODELNAME_' + uniqueDigits + '.jpg';  

Save as .jsx in script folder of Photoshop CC 2019: -->

Then I add the script into my action:

  1. Convert to procfile current document
  2. Convert mode
  3. image size
  4. set background
  5. make text layer
  6. flatten image
  7. Scripts ((this is where I found the script by FILE>SCRIPTS>BROWSE))
  8. Save As - ((here I do not change document name, just save as the original title))
  9. close

Maybe it's in the Save As & Close steps that there is an issue, but when I delete these steps the file then doesn't save at all

When I run your script with the steps above but with Save As & Close, it runs all parts of the action except changing the name. And the files save it still says DSC at final output.


At least on my part, the assumption/expectation was that the code snippets would be incorporated into a larger script, not referenced in an action as is.

As Chuck said, you would need to add code to save as a JPEG and reference the name with additional code.

An alternative is to use additional code that will duplicate using the required new filename, then an action or a file processing script could be used to save the file/s.

EDIT – Something like this, where the original file is duplicated with the new static+variable name and the original file is closed:

#target photoshop

// Dupe Original File with Prefix.jsx

var origDoc = app.activeDocument

var uniqueDigits = origDoc.name.replace(/(^.+_)(\d+)(\.[^\.]*$)/, '$2');

var newName = 'MODELNAME_' + uniqueDigits;

origDoc.duplicate((newName), true); // Change to false to retain layers

// (SaveOptions.SAVECHANGES)

origDoc.close(SaveOptions.DONOTSAVECHANGES);

3 replies

Chuck Uebele
Community Expert
Community Expert
August 5, 2019

With my script, you can get rid of the code getting the path of the current file, and replace it with code for you selected folder. You need to not have the batch save the file, but the line of code I have for saving.

Stephen Marsh
Community Expert
Community Expert
August 5, 2019

Chuck mentions swapping out the code for the backing file path to use another file path. Two options are below...

The first option is interactive and would not be suitable for batch processing using actions, so I have //commented it out, however, it would be useful if put into a loop to create a simple batch script.

The second option uses a cross-platform path to the logged-in user's Desktop.

Otherwise, you would hard code in the platform-specific path to the save folder.

var doc = app.activeDocument;

// var saveFolder = Folder.selectDialog("Select the output folder", ""); // Select the save folder

var saveFolder = "~/Desktop"; // Save to the Desktop (cross platform)

var jpgOptions = new JPEGSaveOptions();

jpgOptions.quality = 10;

var name1 = doc.name.split('_')[1];//gets rid of prefix  

var newName = 'MODELNAME_' + name1.split('.')[0] + '.jpg';//gets rid of old extension

doc.saveAs (new File(saveFolder +'/' + newName), jpgOptions);

Stephen Marsh
Community Expert
Community Expert
August 5, 2019

The following is a hack job, so it may not be as concise as it could be, however it appears to work as intended. This script is self-contained, it will ask for an input and output folder and then batch rename and save, so no need for an action, batch etc.

EDIT: Added error checking to the input/output folder dialog cancel button... but how can I error check and abort if the input folder is empty?

// https://forums.adobe.com/thread/2643049 

// https://forums.adobe.com/message/11198256#11198256 

// Batch renames from "TEXT_123.psd" or "filename_9876" etc. to "MODELNAME_456.jpg" via user defined input & output folders 

#target photoshop

// Select the source folder

var inFolder = Folder.selectDialog('Select the input folder', '');

// Abort script if dialog is cancelled

if ( inFolder != null )

// Select the destination folder

var outFolder = Folder.selectDialog('Select the output folder', '');

// Abort script if dialog is cancelled

if ( outFolder != null )

// Script Execution Timer - Function   

var timeDiff = {   

    setStartTime:function (){d = new Date(); time  = d.getTime();},   

    getDiff:function (){d = new Date(); t = d.getTime() - time; time = d.getTime(); return t;}   

}; 

  

// Script Execution Timer - Start   

timeDiff.setStartTime();

// Loop through input files

// ??? Add Error Checking to Abort script if inFolder is empty ???

var inFiles =  inFolder.getFiles();

for (var a = 0; a < inFiles.length; a++){     

    try {     

var d = open(inFiles);

// Disable dialogs

app.displayDialogs = DialogModes.NO; 

// JPEG Save Options

var jpgOptions = new JPEGSaveOptions();   

jpgOptions.quality = 10;     

/*

jpgOptions.embedColorProfile = false;

jpgOptions.formatOptions = FormatOptions.STANDARDBASELINE;

jpgOptions.matte = MatteType.NONE;

*/

// Rename input files

var doc = app.activeDocument;   

var consecutiveDigits = doc.name.replace(/(^.+_)(\d+)(\.*[^\.]*$)/g, '$2'); 

var newName = 'MODELNAME_' + consecutiveDigits + '.jpg';     

// Save files using new names

doc.saveAs (new File(outFolder + '/' + newName), jpgOptions);   

    }     

        catch(e) {     

        continue;     

    }     

};   

   

// Close all open documents   

while (app.documents.length) {   

    app.activeDocument.close()   

}   

   

// Script Execution Timer - Finish   

alert('Batch rename and save completed!' + '(' + timeDiff.getDiff() / 1000 + ' seconds)');

Chuck Uebele
Community Expert
Community Expert
August 4, 2019

Yes, you can save it is notepad, as long as you save it as plain text - no formatting. Here is a script that will save the file.

var doc = app.activeDocument; 

var docPath = new Folder(doc.path);//get the current folder of the open file

var jpgOptions = new JPEGSaveOptions();

jpgOptions.quality = 10;

var name1 = doc.name.split('_')[1];//gets rid of prefix 

var newName = 'MODELNAME_' + name1.split('.')[0] + '.jpg';//gets rid of old extension

doc.saveAs (new File(docPath +'/' + newName), jpgOptions);

Known Participant
August 5, 2019

Thank you Chuck Uebele, script is working now, and renames all photos the way I need it!!

My last question is that files are being saved in the original folder and not into the folder I have selected when I run the batch processing program.

Not a huge deal as the files now have a new name and I can sort that way, but I didn't know if there was one more line of script that is needed or if I have to have another line of script that allows the file to move into a new folder. I've tried having "override save as" clicked both on and off, but neither seems to do the trick.

Thanks again

Chuck Uebele
Community Expert
Community Expert
August 4, 2019

To split the file name use:

var doc = activeDocument;

var name = doc.name.split('_')[1];//gets rid of prefix

var newName = 'custom name_' + name.split('.')[0] + '.jpg';

/*second split gets rid of file extension.

On the new name, you will need to add a file extension type.

*/

Stephen Marsh
Community Expert
Community Expert
August 4, 2019

As I learned regular expressions before extendscript/javascript, here is a regex version of Chuck's script:

var doc = app.activeDocument;

// regular expression capture group find/replace for one or more consecutive digits

var uniqueDigits = doc.name.replace(/(^.+_)(\d+)(\.[^\.]+$)/, '$2');

// change 'custom name_' & '.jpg' strings as required

var newName = 'custom name_' + uniqueDigits + '.jpg';

Known Participant
August 4, 2019

Thank you!

Maybe I'm not understanding how to add the script into my action because the action is running but it is not following the script you have helped make for me.

This is what I have done, I am using NOTEPAD and saving as a .jsx file on Windows 10.   **and not as .jsx.txt only .jsx

If Notepad is the wrong program let me know.

The steps I follow are below:

I take your script, copy into notepad: -->

  1. var doc = app.activeDocument; 
  2. // regular expression capture group find/replace for one or more consecutive digits 
  3. var uniqueDigits = doc.name.replace(/(^.+_)(\d+)(\.[^\.]+$)/, '$2');  
  4. // change 'custom name_' & '.jpg' strings as required 
  5. var newName = 'custom name_' + uniqueDigits + '.jpg';  

Changed your line #5 to my own text: -->

   5. var newName = 'MODELNAME_' + uniqueDigits + '.jpg';  

Save as .jsx in script folder of Photoshop CC 2019: -->

Then I add the script into my action:

  1. Convert to procfile current document
  2. Convert mode
  3. image size
  4. set background
  5. make text layer
  6. flatten image
  7. Scripts ((this is where I found the script by FILE>SCRIPTS>BROWSE))
  8. Save As - ((here I do not change document name, just save as the original title))
  9. close

Maybe it's in the Save As & Close steps that there is an issue, but when I delete these steps the file then doesn't save at all

When I run your script with the steps above but with Save As & Close, it runs all parts of the action except changing the name. And the files save it still says DSC at final output.