Sathish Acumen
Sathish Acumen

Reputation: 13

check the type of files is present or not using nodejs

I want to find the type of files which is present or not, I am using nodejs, fs. Here is my code

var location = '**/*.js';
log(fs.statSync(location).isFile());

which always returns the error.

 Error: ENOENT, no such file or directory '**/*.js'

How to I find the files is present or not. Thanks in Advance.

Upvotes: 1

Views: 54

Answers (2)

It-Z
It-Z

Reputation: 2000

node.js dose not support "glob" wildcards by default. You can use external package like this one

Upvotes: 0

Justin Doyle
Justin Doyle

Reputation: 979

node doesn't have support for globbing (**/*.js) built-in. You'll need to either recursively walk the directories and iterate over the array of file names to find the file types you want, or use something like node-glob.

Using recusrive-readdir-sync

var recursiveReadSync = require('recursive-readdir-sync'),
    files;
files = recursiveReadSync('./');

files.forEach(function (fileName) {
    if (fileName.search(/\.js$/g) !== -1) {
        console.log("Found a *.js file");
    }
});

Using node-glob:

var glob = require("glob")
glob("**/*.js", function (er, files) {
    files.forEach(function (fileName) {
    if (fileName.search(/\.js$/g) !== -1) {
        console.log("Found a *.js file");
    }
});

Upvotes: 1

Related Questions