Skip to main content
Participating Frequently
November 17, 2020
Question

Script For Opening Next Sequential File From Folder

  • November 17, 2020
  • 1 reply
  • 634 views

I am currently working on an action that takes an opened jpg file, creates a new document, and pastes that file into the document. The action will then use a script to open the next sequential jpg file and paste that file into the created document. It will then do this recursively for the next 7 files, ultimately having 9 files loaded into that document.

 

I found a script that takes a file (called "00000.jpg") and will open the next file ("00001.jpg") in photoshop. This script opens the next file perfectly, but my problem is that I would have to batch rename all of my jpg files to follow this naming rule. For instance, I'm creating this action to use for thousands of files. These files are separated into their own respective folders and the folders could have anywhere from 30-500 pictures in them. These folders have different starting file names and one could be name "image5352.jpg." I need a script that can take any file, using this name rule, and open the next sequential file (in this case "image5353.jpg") into photoshop. 

 

This is the script that I found that opens the next file using the "00000.jpg" name rule:

var folderPath = activeDocument.path
var filePieces = activeDocument.name.split('.');
var fileName = filePieces[0];
var fileExtension = filePieces[1];
// We need to unpad the string for some reason, otherwise PS-JS
// thinks it's a past layer some times when using parseInt. 
fileNameStart = 0;
for (var i = 0; i < fileName.length - 1; i++) {
    digit = fileName[i];
    if (digit == '0') {
        fileNameStart++;
    } else {
        break
    };
}
fileNameUnpadded = fileName.slice(fileNameStart);
var nextFileNumber = parseInt(fileNameUnpadded) + 1;
// Yes it's haxxy I know...
var padTemp = ("000000000000000" + nextFileNumber.toString());
var nextFileName = padTemp.slice(padTemp.length - 5);
var nextFilePath = folderPath + '/' + nextFileName + '.' + fileExtension;
activeDocument.close(SaveOptions.SAVECHANGES);

try {
    var docRef = open(new File(nextFilePath));
}
catch (e) {
    alert("No more files in the directory!");
}

 

Thank you for the help

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
November 24, 2020

Yes, this is possible using different code.

 

However, my questions include:

 

* Why create a new doc? Why not just combine 8 images into the 9th file as layers?

 

* Do the various images all have the same pixel depth and width?

 

* Do all of the files have a single layer or a flattened background layer?

 

* Do the files have to be stacked as layers in a certain order, or do you need to use various opacity values or layer blend modes etc?

 

* What to do with the first combined 9 layer file, before moving onto the next 9 files? Save into what file format and options?

 

* Do the layers need to have specific names?

 

* Do the saved files need to have specific names?

 

* Do the saved files need to be in the same folder or a new sub-folder in the same folder or a different folder?

 

Perhaps you can illustrate your request with screenshots of the 9 layers of the final combined result and the 9 separate source files that created the combined file.