Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Explorer ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

#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

Views

663

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

Adobe Community Professional , 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...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

 

#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

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

Copy link to clipboard

Copied

Thanks for the code you edited, it works fine

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

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

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

LATEST

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

Copy link to clipboard

Copied

Use:

fileList[a].name

or:

fileList.shift().name

 

 

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