get list of files in a folder as array?

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

edsaye

get list of files in a folder as array?

Post by edsaye »

I would like to build a script that will ask the user to point to a folder, and from that folder the script would get the count of the files in that folder and put the names into an array. Is this possible? If so I'd then like to have the script cycle through each of them to perform a task.
undavide

get list of files in a folder as array?

Post by undavide »

Here you go:

Code: Select allvar selectedFolder = Folder.selectDialog ("Choose a folder");
var fileArray = selectedFolder.getFiles(); //Array of File objects
var nameArray = []; // Array of Strings
for (var i = 0; i < fileArray.length; i++) {
    nameArray.push(File.decode(fileArray.name));   
}

Then you can cycle through the nameArray - even though it might be more useful to deal with the fileArray.
See the Javascript Tools Guide (section 3, File System Access pag. 39) for a complete documentation.
Regards

Davide Barranca
http://www.davidebarranca.com
http://www.cs-extensions.com