List all files in folder and subfolders

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

User avatar
Scriptor
Posts: 18
Joined: Tue Oct 01, 2019 12:07 pm

List all files in folder and subfolders

Post by Scriptor »

Hey guys!
Im trying to get a list of all files (doesn't really matter what extension) that exists in a folder and it's subfolders.
So far I've tried a few solutions without coming up with anything simple and reliable. I did try this;
https://community.adobe.com/t5/photosho ... -p/5162230

But it returns that "sFolders.getFiles(); is not a function.", and since it's friday afternoon I guess my brain is mush.

Anyhow - I just want a simple array containing all files (including their searchway) based on whatever input folder you select.
How would I achieve this?

User avatar
Kukurykus
Posts: 453
Joined: Mon Jul 25, 2016 12:36 pm

Re: List all files in folder and subfolders

Post by Kukurykus »

After Adobe transited their forums from old platform, the parts of codes like for ex. were lost.

User avatar
Scriptor
Posts: 18
Joined: Tue Oct 01, 2019 12:07 pm

Re: List all files in folder and subfolders

Post by Scriptor »

Relized someone else might be interrested in this. Here's an overly complex solution that I made shortly after posting the question.
I know it's really ugly, but at the time I just wanted something that did the job, and I'm not too concerned about how long time it takes to get all files.

Enjoy!

Code: Select all

	var ReturnArray = [];
	var GlobalFiles = [];
	var GlobalFolders = [];
	var allFoldersList = getAllFolders(Folder.selectDialog("Select Inputfolder"));
		for (i = 0; i < allFoldersList.length; i++) {
			//alert("Searching in folder:\n" + allFoldersList[i]);
			
			var FolderContentArray = new Folder(allFoldersList[i]);
			var FolderContentArray = FolderContentArray.getFiles(); //Array of File objects
			for (y = 0; y < FolderContentArray.length; y++) {											//For each array item
				GlobalFiles.push(FolderContentArray[y] + "\n");
			}
		}
	alert(GlobalFiles);	//Do whatever you want with the output
	
	
	function getAllFolders(selectedFolder){
		var displayerror = 0;
		if(selectedFolder == null) {alert("No folder selected.");return;}
		var folderList = [];
		GlobalFolders.push(selectedFolder);
		
		try{
			folderList[0] = selectedFolder.getFiles(function(f) { return f instanceof Folder; }); 
			GlobalFolders.push(folderList[0]);
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		try{
			folderList[1] = []; 
			for (y = 0; y < folderList[0].length; y++) {
				if (y == 0) {
					folderList[1] = folderList[1] + folderList[0][y].getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[1] = folderList[1] + "," + folderList[0][y].getFiles(function(f) { return f instanceof Folder; });
				}
			}
			GlobalFolders.push(folderList[1]);
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}	
		
		try{
			folderList[1] = folderList[1].split(",");
			folderList[2] = [];
			for (y = 0; y < folderList[1].length; y++) {
				var tempfile = new Folder(folderList[1][y]);
				if (y == 0) {
					folderList[2] = folderList[2] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[2] = folderList[2] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[2] = folderList[2].split(",");
			GlobalFolders.push(folderList[2]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			folderList[3] = [];
			for (y = 0; y < folderList[2].length; y++) {
				var tempfile = new Folder(folderList[2][y]);
				if (y == 0) {
					folderList[3] = folderList[3] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[3] = folderList[3] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[3] = folderList[3].split(",");
			GlobalFolders.push(folderList[3]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[3].length; y++) {
				var tempfile = new Folder(folderList[3][y]);
				if (y == 0) {
					folderList[4] = folderList[4] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[4] = folderList[4] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[4] = folderList[4].split(",");
			GlobalFolders.push(folderList[4]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[4].length; y++) {
				var tempfile = new Folder(folderList[4][y]);
				if (y == 0) {
					folderList[5] = folderList[5] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[5] = folderList[5] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[5] = folderList[5].split(",");
			GlobalFolders.push(folderList[5]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[5].length; y++) {
				var tempfile = new Folder(folderList[5][y]);
				if (y == 0) {
					folderList[6] = folderList[6] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[6] = folderList[6] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[6] = folderList[6].split(",");
			GlobalFolders.push(folderList[6]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[6].length; y++) {
				var tempfile = new Folder(folderList[6][y]);
				if (y == 0) {
					folderList[7] = folderList[7] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[7] = folderList[7] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[7] = folderList[7].split(",");
			GlobalFolders.push(folderList[7]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[7].length; y++) {
				var tempfile = new Folder(folderList[7][y]);
				if (y == 0) {
					folderList[8] = folderList[8] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[8] = folderList[8] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[8] = folderList[8].split(",");
			GlobalFolders.push(folderList[8]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[8].length; y++) {
				var tempfile = new Folder(folderList[8][y]);
				if (y == 0) {
					folderList[9] = folderList[9] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[9] = folderList[9] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[9] = folderList[9].split(",");
			GlobalFolders.push(folderList[9]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[9].length; y++) {
				var tempfile = new Folder(folderList[9][y]);
				if (y == 0) {
					folderList[10] = folderList[10] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[10] = folderList[10] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[10] = folderList[10].split(",");
			GlobalFolders.push(folderList[10]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		try{
			for (y = 0; y < folderList[10].length; y++) {
				var tempfile = new Folder(folderList[10][y]);
				if (y == 0) {
					folderList[11] = folderList[11] + tempfile.getFiles(function(f) { return f instanceof Folder; });
				} else {
					folderList[11] = folderList[11] + "," + tempfile.getFiles(function(f) { return f instanceof Folder; });
				}
			folderList[11] = folderList[11].split(",");
			GlobalFolders.push(folderList[11]);
			}
		} catch(error) {
			if (displayerror == 1) {
				alert(error);
			}
		}
		
		var tempitem = GlobalFolders.toString('');
		var temparray = tempitem.split(",");
		var ReturnArray = [temparray[0]];
		var identical = 0;
		
		for (var i = 0; i < temparray.length; i++) {  
			for (x = 0; x < ReturnArray.length; x++) {
				if (identical == 1) {break;}
				var string1 = temparray[i].toString('');
				var string2 = ReturnArray[x].toString('');
				if (StringMatches(string1, string2)) {
					identical = 1;
				}
			}
			if (identical === 0){
				var identical = 0;
				ReturnArray.push(temparray[i].toString(''));
			} else {
				var identical = 0;
			}
		}
		return ReturnArray;
	}