Node.js: How to check if folder is empty or not with out uploading list of files

jsist picture jsist · Jan 29, 2013 · Viewed 13.7k times · Source

I am using Node.js.

I want to check if folder is empty or not? One option is to use fs.readdir but it loads whole bunch of files into an array. I have more than 10000 files in the folder. Loading files name is useless just to check if folder is empty or not. So looking for alternate solution.

Answer

Jonathan Grupp picture Jonathan Grupp · Nov 6, 2014

How about using nodes native fs module http://nodejs.org/api/fs.html#fs_fs_readdir_path_callback. It's readdir and readdirSync functions provide you with an array of all the included file names (excluding . and ..). If the length is 0 then your directory is empty.