javascriptfirefoxxpcom

Hot to list all files from a folder with XPCOM


I want to list all images from a folder using XPCOM and iMacros. Bellow is the code I use but I get an error.

ReferenceError: file is not defined, line 319 (Error code: -991)

Here is example of code I am using.

   var imageurl =  "s:\\images\\";

imageurl = CheckFolder(imageurl);

alert(imageurl);

function CheckFolder(path) {

            alert(path)
            file.initWithPath(path);
            var children = file.directoryEntries;
            var child;
            var list = [];
            while (children.hasMoreElements()) {
                child = children.getNext().QueryInterface(Components.interfaces.nsILocalFile);
                list.push(child.leafName + (child.isDirectory() ? ' [DIR]' : ''));
            }
            alert(list.join('\n'));
        }

Solution

  • This is the solution I've made from link above. You were right on the target.

    //find all files in folder

    function CheckFolder(filePath) {
    
    
    
        var nsFile = Components.Constructor("@mozilla.org/file/local;1", "nsIFile", "initWithPath");
    
        var file = new nsFile(filePath);
    
        //file.initWithPath(filePath);
        var children = file.directoryEntries;
        var child;
        var list = [];
        while (children.hasMoreElements()) {
            child = children.getNext().QueryInterface(Components.interfaces.nsILocalFile);
            list.push(child.leafName + (child.isDirectory() ? ' [DIR]' : ''));
        }
        alert(list.join('\n'));
    
        return list;
    }