Loop all subfolders - mask folders with specific name

Engaged ,
Apr 24, 2021 Apr 24, 2021

Copy link to clipboard

Copied

Hello everyone,

I have this code which is working, it loops all subfolders and it only enters files with the file extension I wrote above (NEF & tif).

 

I want the mask to enter the code only if the subfolder is "TEST", is this possible or do I have to loop again on all subfolders and write 'if conditional' inside the loop? (which will take more time probably)

 

 

#target photoshop

var theFolder = Folder.selectDialog("select folder");

var tifFilesAr = scanSubFolders(theFolder,/\.(tif)$/i);				//var fileandfolderAr = scanSubFolders(topFolder,/\.(jpg|tif|psd|bmp|gif|png|)$/i); 
var tifList = tifFilesAr[0];

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

var fileList = fileandfolderAr[0];


for(var a = 0 ;a < fileList.length; a++)
{
	// code 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[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];
};

 

Thanks in advance.

TOPICS
Actions and scripting, How to, Windows

Views

152

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Enthusiast , Apr 25, 2021 Apr 25, 2021
Your code already has an example of how to do this for the filename. Just add a similar check but with a different mask. if (procFiles[i].fullName.search(mask) != -1) (if you have a lot of files in a directory, it is betterto search substring with .indexOf for performance instead of searching with regular expressions)

Likes

Translate

Translate
Adobe Community Professional ,
Apr 25, 2021 Apr 25, 2021

Copy link to clipboard

Copied

I think you would only need to amend the line 

        }else if (procFiles[i] instanceof Folder){

to iclude a name-check. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 25, 2021 Apr 25, 2021

Copy link to clipboard

Copied

How would you do it please?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 25, 2021 Apr 25, 2021

Copy link to clipboard

Copied

With the method »match«, I guess. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 25, 2021 Apr 25, 2021

Copy link to clipboard

Copied

LATEST

Your code already has an example of how to do this for the filename. Just add a similar check but with a different mask.

 

if (procFiles[i].fullName.search(mask) != -1)

 

(if you have a lot of files in a directory, it is betterto search substring with .indexOf for performance instead of searching with regular expressions)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines