Skip to main content
Participating Frequently
June 25, 2021
Answered

How to get the names of all files in a directory to array

  • June 25, 2021
  • 2 replies
  • 2978 views
#target photoshop;
main();
function main(){
var selectedFolder = Folder.selectDialog("Please select  folder");
if(selectedFolder == null) return;
var fileList= selectedFolder.getFiles();
if(fileList.length>0){
var out = new File(Folder.desktop + "/out.txt");
out.open("w");
out.encoding="UTF8";
for(var a in fileList){out.writeln(decodeURI(fileList.name));}
out.close();
}
};

have a folder with more than 1000 jsx files with different filenames, I want to create a text file on the desktop that contains all the files in the folder, but I'm getting errors, please correct me, I'm sorry Thank you

In folder

name1.jsx , name2.jsx, name3.jsx....namen.jsx

I want to get to

["name1","name2",......,"namen"]

This topic has been closed for replies.
Correct answer JJMack

 

#target photoshop;
main();
function main(){
	var selectedFolder = Folder.selectDialog("Please select  folder");
	if(selectedFolder == null) return;
	var fileList= selectedFolder.getFiles();
	if(fileList.length>0){
		var names = "";
		for (var i = 0; i < fileList.length; i++) {  
			if (i==0 )   names =  '"' + decodeURI(fileList[i].name) + '"';
			else names = names + '\n"' + decodeURI(fileList[i].name) + '"';
		}
		logInfo(names)
	}
}
	

function logInfo(Txt){
    try {	
       var file = new File(Folder.desktop + "/out.txt"); 
       file.open("w", "TEXT", "????"); 
       //alert(file.encoding);
       file.encoding = "UTF8";
       file.seek(0,2);   
       $.os.search(/windows/i)  != -1 ? file.lineFeed = 'windows'  : file.lineFeed = 'macintosh';
       file.writeln(Txt); 
       if (file.error) alert(file.error);
       file.close();
       file.execute(); 
    }
    catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
};   

 

chamge the \n linefeed to , if you want a single line text file

2 replies

Kukurykus
Legend
June 25, 2021

Use:

fileList[a].name

or:

fileList.shift().name

 

 

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
June 25, 2021

 

#target photoshop;
main();
function main(){
	var selectedFolder = Folder.selectDialog("Please select  folder");
	if(selectedFolder == null) return;
	var fileList= selectedFolder.getFiles();
	if(fileList.length>0){
		var names = "";
		for (var i = 0; i < fileList.length; i++) {  
			if (i==0 )   names =  '"' + decodeURI(fileList[i].name) + '"';
			else names = names + '\n"' + decodeURI(fileList[i].name) + '"';
		}
		logInfo(names)
	}
}
	

function logInfo(Txt){
    try {	
       var file = new File(Folder.desktop + "/out.txt"); 
       file.open("w", "TEXT", "????"); 
       //alert(file.encoding);
       file.encoding = "UTF8";
       file.seek(0,2);   
       $.os.search(/windows/i)  != -1 ? file.lineFeed = 'windows'  : file.lineFeed = 'macintosh';
       file.writeln(Txt); 
       if (file.error) alert(file.error);
       file.close();
       file.execute(); 
    }
    catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
};   

 

chamge the \n linefeed to , if you want a single line text file

JJMack
Participating Frequently
June 25, 2021

Thanks for the code you edited, it works fine

JJMack
Community Expert
Community Expert
June 25, 2021

I only hack at scripting I do not know javascript.  I knew your code had  to process the fileList array elements so I hack it a way I knew how to.  Kukurykus knows javascript well I'm sure what he posted would fix your original code.

 

 

The logging code is code I came across years ago.  I think a single write will perform better then writing names one at a time

JJMack