Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 643
jsist Avatar asked Jan 29 '13 07:01

jsist


People also ask

How do you check whether the file is empty or not in node JS?

Check if a File is Empty with Node. The easiest way to check is to stream the data within the file and check its length. If there are 0 bytes in the file, or rather, if the length of the data is equal to 0 , the file is empty: router.

How do I empty a folder in node JS?

Using fs.rm() and fs.rmSync() If you like the old-fashioned callback things of Node. js, you can use the rm() method to asynchronously remove a directory or use the rmSync() method to synchronously remove a directory.

How do you check if path is file or directory js?

The stats. isDirectory() method returns true if file path is Directory, otherwise returns false.


1 Answers

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.

like image 167
Jonathan Grupp Avatar answered Oct 14 '22 05:10

Jonathan Grupp