Search code examples
javascriptphantomjsfile-listing

PhantomJS fs.list() returning wrong length


I am trying to get the number of files in a directory, but PhantomJS is returning more files than what exists. Am I missing something here? (I have no hidden files in this directory)

testNoOfFiles = fs.list(dir).length; 
console.log(testNoOfFiles);

This logs 7, when I have 5 files in the directory.


Solution

  • . (current directory) and .. (parent directory) are also always part of the list. If you don't want to consider them, you can subtract 2 and always disregard the first two elements of fs.list() output. This is also the case on windows.