0

I have a file structure like this:

 lib |->Code |-> Style |-> style.css 

I want to get style.css file

4
  • Define a path for that file or make the folder static, if you are using express framework Commented Dec 19, 2017 at 11:44
  • 1
    Please give more information - what exactly are you trying to achieve, and what have you tried so far? Commented Dec 19, 2017 at 11:45
  • Check out the docs: nodejs.org/dist/latest-v8.x/docs/api/… Commented Dec 19, 2017 at 11:45
  • Recursively i want to get the file name index.less from all the sub directory Commented Dec 19, 2017 at 11:47

1 Answer 1

3

The following code does a recursive search inside ./ (change it appropriately) and returns an array of absolute file names ending with style.css.

var fs = require('fs'); var path = require('path'); var searchRecursive = function(dir, pattern) { // This is where we store pattern matches of all files inside the directory var results = []; // Read contents of directory fs.readdirSync(dir).forEach(function (dirInner) { // Obtain absolute path dirInner = path.resolve(dir, dirInner); // Get stats to determine if path is a directory or a file var stat = fs.statSync(dirInner); // If path is a directory, scan it and combine results if (stat.isDirectory()) { results = results.concat(searchRecursive(dirInner, pattern)); } // If path is a file and ends with pattern then push it onto results if (stat.isFile() && dirInner.endsWith(pattern)) { results.push(dirInner); } }); return results; }; var files = searchRecursive('./', 'style.css'); // replace dir and pattern // as you seem fit console.log(files); // e.g.: ['C:\\You\\Dir\\subdir1\\subdir2\\style.css'] 

This approach is synchronous.

Sign up to request clarification or add additional context in comments.

1 Comment

Supposing there are 16 directories as a result of the above code i get 16 style.css files.I want to make 16 new directories and save result of array using fs.writeFile

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.