Skip to main content
Participant
May 10, 2023
해결됨

Trying to get the names of Images in a Folder

  • May 10, 2023
  • 1 답변
  • 393 조회

Hey, I am trying to get the names of images in a Folder. Tried using a script like this but got an error saying folder.getFiles is not a function. How can I do this ?

"""

var myFolder = Folder.selectDialog();
alert(myFolder)
    if(folder != null)
    {
        var files = folder.getFiles(/\.(jpg|jpeg|png|gif)$/i);
        alert(files.length)
    //     for(var i = 0; i < files.length; i++)
    //     {
    //         var file = files[i];
    //         if(file instanceof File && )
    //     }
    }
""""
이 주제는 답변이 닫혔습니다.
최고의 답변: rob day

Hi @Victor29751988fsbx , You defined the myFolder variable, but then referenced an undefined variable folder. Try this:

 

var fldr = Folder.selectDialog("Select the folder containing the images", ""); 
var na = []
if(fldr != null){ 
    var fa = fldr.getFiles(/\.(jpg|jpeg|png|gif)$/i); 
    for (var i = 0; i < fa.length; i++){
        na.push(fa[i].name)
    };   
}

$.writeln(na)
//returns an array of file names

 

1 답변

rob day
Community Expert
rob dayCommunity Expert답변
Community Expert
May 10, 2023

Hi @Victor29751988fsbx , You defined the myFolder variable, but then referenced an undefined variable folder. Try this:

 

var fldr = Folder.selectDialog("Select the folder containing the images", ""); 
var na = []
if(fldr != null){ 
    var fa = fldr.getFiles(/\.(jpg|jpeg|png|gif)$/i); 
    for (var i = 0; i < fa.length; i++){
        na.push(fa[i].name)
    };   
}

$.writeln(na)
//returns an array of file names