0

again im stack with node.js and need your help.

i try to write script for cmd that search for all sub directory under current dir(process.cwd), and print just the one's that contain a string the user has given me (with process.argv[2]).

i was looking here for some answar without succsess. also try do it with 'stat.isDirectory' and 'fs.readdir', but im still need to learn way more to do it.

hope that someone can help me,

thanks anyway, eyal

Eyal Cohen
  • 1,188
  • 2
  • 15
  • 27
  • check http://stackoverflow.com/questions/18112204/get-all-directories-within-directory-nodejs#24594123 – mestarted Jan 26 '16 at 12:54
  • ok, thanks. but how i print just the one's that contain the string the user given me? – Eyal Cohen Jan 26 '16 at 13:01
  • Why don't you loop through the directory names and filter that..like `var userStr = 'user_provided_string'; getDirectories('.').forEach(function(dirName,i){ if(dirName.indexOf(userStr) > -1){ console.log(dirName); return; } });` – mestarted Jan 26 '16 at 13:33
  • like that? https://jsfiddle.net/eyal4/etfc8unp/ , still dont print anything. maybe if i put all the sub dirs in array and then do a loop ? – Eyal Cohen Jan 26 '16 at 13:43
  • not like this, `getDirectories` is synchronous, you can call it outside that function.. move the code to the outside the `getDirectories` function. check https://jsfiddle.net/5ycm5uxd/1/ but i am not sure about the output. – mestarted Jan 26 '16 at 13:46
  • maybe its the right answar but still dont print anything. there is another way? ive been try also to print it with che stdout. – Eyal Cohen Jan 26 '16 at 13:56
  • why are you trying this server code in client side, try this in node server – mestarted Jan 26 '16 at 13:56
  • this is a mission ive had to do for a class. what do you mean by in node server? its supposed to be on cmd. – Eyal Cohen Jan 26 '16 at 14:22
  • i mean this is a server side script, should run on server. this will not work on client side. – mestarted Jan 27 '16 at 05:50

1 Answers1

0

I've used getDirectories function from this link Get all directories within directory nodejs

try this code in server side

var fs = require('fs'),path = require('path');
var userStr = 'user_provided_string'; 

function getDirectories(srcpath) {
  return fs.readdirSync(srcpath).filter(function(file) {
    return fs.statSync(path.join(srcpath, file)).isDirectory();
  });
}

getDirectories('.').forEach(function(dirName,i){
    if(dirName.indexOf(userStr) > -1){ 
       console.log(dirName); 
       return; 
    } 
});
Community
  • 1
  • 1
mestarted
  • 413
  • 4
  • 11