Quantcast
Channel: Adobe Community: Message List - Photoshop Scripting
Viewing all articles
Browse latest Browse all 27456

Re: Photoshop javascript: Open files in all subfolders

$
0
0

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[j].getFiles();         for (var i=0;i<procFiles.length;i++){ // loop through this folder contents             if (procFiles[i] instanceof File ){                if(mask==undefined) allFiles.push(procFiles[i]);// if no search mask collect all files                if (procFiles[i].fullName.search(mask) != -1) allFiles.push(procFiles[i]); // otherwise only those that match mask        }else if (procFiles[i] instanceof Folder){            sFolders.push(procFiles[i]);// store the subfolder            scanSubFolders(procFiles[i], mask);// search the subfolder         }      }    }    return [allFiles,sFolders]; 
};

Viewing all articles
Browse latest Browse all 27456

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>