Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Jun 24, 2021 Jun 24, 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 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"]

TOPICS
Actions and scripting
2.9K
Translate
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

Community Expert , Jun 24, 2021 Jun 24, 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 F
...
Translate
Adobe
Community Expert ,
Jun 24, 2021 Jun 24, 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
Translate
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
Explorer ,
Jun 24, 2021 Jun 24, 2021

Thanks for the code you edited, it works fine

Translate
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
Community Expert ,
Jun 25, 2021 Jun 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
Translate
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
LEGEND ,
Jun 25, 2021 Jun 25, 2021
LATEST
Translate
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
LEGEND ,
Jun 24, 2021 Jun 24, 2021

Use:

fileList[a].name

or:

fileList.shift().name

 

 

Translate
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