Scanning a System of Folders for Files and Folders

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Kukurykus, Tom, xbytor

Andrew

Scanning a System of Folders for Files and Folders

Post by Andrew »

This is in response to a question at the Adobe forum on how to scan a system of folders for the file and folder contents. I have just recently changed the way I do this having discovered that JS is surprisingly slow at doing folder scans. On my very fast video-editing PC it takes PS JS 20 seconds to scan a system of folders with 2,500 files in it. If I scan the same system with Windows search, it takes less than 1 second (presumably because XP has built a cache of the contents, and PS JS presumably does not access that cache).

Having realised that I altered my code to prevent it ever scanning the file system twice (previously I scanned first for folders and scanned each folder individually - that takes 40 seconds).

Here is my latest version - obviously this can be done many ways.

Code: Select allfunction scanSubFolders(tFolder)
{
   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 instanceof File) allFiles.push(procFiles);
         else if (procFiles instanceof Folder) sFolders.push(procFiles);
      }
   }
   return [allFiles,sFolders];
}
   
var topFolder  = new Folder ('/e/photo20d/test');
var fileandfolderAr = scanSubFolders(topFolder);
alert('Scan of ' + topFolder.fullName + '\n' + fileandfolderAr[0].length + ' files\nLast File: ' + fileandfolderAr[0][fileandfolderAr[0].length-1]);
alert('Scan of ' + topFolder.fullName + '\n' + fileandfolderAr[1].length + ' folders\nLast Folder: ' + fileandfolderAr[1][fileandfolderAr[1].length-1]);

Andrew