Skip to main content
Participating Frequently
July 2, 2013
Answered

Photoshop javascript: Open files in all subfolders

  • July 2, 2013
  • 3 replies
  • 35866 views

Hi guys

I'm very new to javascript, and general Photoshop scripting. My script is coming on well, but I struggle working with files and folders. It's a lack of basic javascript knowledge, which I need to work on. Right now, I'm trying to expand the image open part of my script, so that it opens images regardless of whether they are in a subfolder or not.

You can see my input folder (C:/Input). I want to be able to throw files in there, some in folders, some not. Out of interest, my outputs will be combined into one output folder... that bit is fine.

var inFolder = new Folder("C:/Input")

if(inFolder != null){

var fileList = inFolder.getFiles(/\.(jpg|tif|psd|bmp|gif|png|)$/i);

}

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

{

var docRef = open(fileList);

//do things here

}

Is there an easy way of expanding this to include files in subfolders? All advice greatly appreciated.

Thanks

David

This topic has been closed for replies.
Correct answer Michael_L_Hale

Thank you Michael and Pascalculator for your explanations, I'm very grateful. I've struggled a little to integrate either into my script - I will close myself in a dark room later and work on it. I'll get back when I've figured it!

Thanks

David


You could combine the code you posted with the code I posted like this:

var topFolder = new Folder('~/desktop/test');
var fileandfolderAr = scanSubFolders(topFolder,/\.(jpg|tif|psd|bmp|gif|png|)$/i);

var fileList = fileandfolderAr[0];

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

{

var docRef = open(fileList);

//do things here

}

function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
    var sFolders = new Array();
    var allFiles = new Array();
    sFolders[0] = tFolder;
    for (var j = 0; j < sFolders.length; j++){ // loop through folders            
        var procFiles = sFolders.getFiles();
        for (var i=0;i<procFiles.length;i++){ // loop through this folder contents
            if (procFiles instanceof File ){
                if(mask==undefined) allFiles.push(procFiles);// if no search mask collect all files
                if (procFiles.fullName.search(mask) != -1) allFiles.push(procFiles); // otherwise only those that match mask
        }else if (procFiles instanceof Folder){
            sFolders.push(procFiles);// store the subfolder
            scanSubFolders(procFiles, mask);// search the subfolder
         }
      }
   }
   return [allFiles,sFolders];
};

New Participant
September 28, 2019

Error 4: sFolders.getFiles is not a function.

Line 21

var procFiles = sFolders.getFiles();

New Participant
December 16, 2020

GO to xbytor tools-ActionEvalDemo script-line 8873

July 2, 2013

var traverseFolder = function(path)

{

     // Create new folder object based on path string

     var folder = new Folder(path);

     // Get all files in the current folder

     var files = folder.getFiles();

     // Loop over the files in the files object

     for (var i = 0; i &lt; files.length; i++)

     {

          // Check if the file is an instance of a file

          // else call the traverse folder recursively with the current folder as an argument

          if (files instanceof File)

          {

               // Convert the file object to a string for matching purposes (match only works on String objects)

               var fileString = String(files);

               // Check if the file contains the right extension

               if (fileString.match(/.(jpg|tif|psd|bmp|gif|png)$/))

               {

                    // Do something if the file matches

                    alert(fileString + 'good file');

               }

               else

               {

                    // Do something if the file doesn't match

                    alert(fileString + 'bad file');

               }

          }

          else

          {

               alert('passing folder: ' + files);

               // Call method recursively with the current folder as an argument

               traverseFolder(files);

          }

     }

}

traverseFolder('c:/path');

You can use the traverseFolder() function and pass the path as a parameter. Make sure to delete the alerts, there just there to show you which folder/file is currently being loaded. Also make sure to delete the comments in the for statement, since they cause the program to slow down unneccesarily.

Inspiring
July 2, 2013

Pascalculator wrote:

... make sure to delete the comments in the for statement, since they cause the program to slow down unneccesarily.

The comments will not effect the speed of the script as it runs. If the comments help you remember what the script does, how it does what it does, or how to use it I suggest leaving them in.

Participating Frequently
July 3, 2013

Thank you Michael and Pascalculator for your explanations, I'm very grateful. I've struggled a little to integrate either into my script - I will close myself in a dark room later and work on it. I'll get back when I've figured it!

Thanks

David

Inspiring
July 2, 2013

Folder.getFiles() only works with one folder at a time. To get all the files in the folder as well as in subfolders you need to scan each subfolder. One way to do that is with a recursive function. Something like this:

var topFolder = new Folder('~/desktop/test');

var fileandfolderAr = scanSubFolders(topFolder,/\.(jpg|tif|psd|bmp|gif|png|)$/i);

alert('Scan of ' + topFolder.fullName + '\n' + fileandfolderAr[0].length + ' files\nLast File: ' + decodeURI(fileandfolderAr[0][fileandfolderAr[0].length-1]));

alert('Scan of ' + topFolder.fullName + '\n' + fileandfolderAr[1].length + ' folders\nLast Folder: ' + decodeURI(fileandfolderAr[1][fileandfolderAr[1].length-1]));

function scanSubFolders(tFolder, mask) { // folder object, RegExp or string

    var sFolders = new Array();

    var allFiles = new Array();

    sFolders[0] = tFolder;

    for (var j = 0; j < sFolders.length; j++){ // loop through folders            

        var procFiles = sFolders.getFiles();

        for (var i=0;i<procFiles.length;i++){ // loop through this folder contents

            if (procFiles instanceof File ){

                if(mask==undefined) allFiles.push(procFiles);// if no search mask collect all files

                if (procFiles.fullName.search(mask) != -1) allFiles.push(procFiles); // otherwise only those that match mask

        }else if (procFiles instanceof Folder){

            sFolders.push(procFiles);// store the subfolder

            scanSubFolders(procFiles, mask);// search the subfolder

         }

      }

   }

   return [allFiles,sFolders];

};

davidc88034496
Known Participant
September 11, 2020

this is an excellent answer but you forgot to put the index numbers in in Files within the loop... it should look like in the code below.

 

 

function traverseFolder(path) {

// Create new folder object based on path string
var folder = new Folder(path);

// Get all files in the current folder
var files = folder.getFiles();

// Loop over the files in the files object
for (var i = 0; i < files.length; i++) {

// Check if the file is an instance of a file
// else call the traverse folder recursively with the current folder as an argument

if (files[i] instanceof File) {

// Convert the file object to a string for matching purposes (match only works on String objects)
var fileString = String(files[i]);
// Check if the file contains the right extension

if (fileString.match(/.(html)$/)) {

// Do something if the file matches
$.writeln(fileString + ' : This is a html file' + "\n");

} else {

// Do something if the file doesn't match
$.writeln(fileString + ' : This is NOT an html file' + "\n");

}

} else {
$.writeln('passing folder: ' + files[i] + "\n");

// Call method recursively with the current folder as an argument
traverseFolder(files[i]);
}

}

}