-2
var dir = "<path>";
var dir1 = new Array();
var files1 = new Array();
dir1 = [];
files1 = [];

function file_tree(dir1, files1) {
  const filesArray = fs.readdirSync(dir).filter(file => fs.lstatSync(dir + file))

  for (let i = 0; i < filesArray.length; i++) {
    // Use stat() method
    fs.stat(dir + filesArray[i], (err, stats) => {
      if (!err) {
        if (stats.isDirectory()) {
          dir1.push(filesArray[i]);
        } else if (stats.isFile()) {
          files1.push(filesArray[i]);
        }
      } else
        throw err;
    });
  }

  console.log(dir1, files1, "return")
  return dir1, files1
}

dir1, files1 = file_tree(dir1, files1)
console.log(dir1)
console.log(files1);

Output:

[] [] return

[]

[]

In my opinion the problem is that the push statement doesn't work globally and I need to run it globally. but I do not know how!?

(If I put console.log(files1), in the if statement, after "push", then the file or dir list will be output)

Goal: That in fils1 all files of the dir (in which I am) are listed and in dir1 all dir of the dir (in which I am) are listed

Barmar
  • 741,623
  • 53
  • 500
  • 612
Test Ware
  • 1
  • 1
  • Use `fs.statSync()` to write synchronous code. – Barmar Aug 24 '23 at 22:12
  • white`fs.statSync()` same result – Test Ware Aug 25 '23 at 12:44
  • `statSync()` doesn't take a callback function, it returns the results. Did you recode in that way? – Barmar Aug 25 '23 at 15:51
  • What do you mean? I add the items to a list. but the list is not passed on to the enveloping function – Test Ware Aug 25 '23 at 16:42
  • I mean did you change from `fs.stat(dir + filesArray[i], (err, stats) => { ... })` to `let stats = fs.statSync(dir + filesArray[i]);` – Barmar Aug 25 '23 at 16:43
  • That looks like a callback function, just as in the original code. It doesn't use a callback. Read the documentation. – Barmar Aug 25 '23 at 16:44
  • ` for ... { // Use stat() method let stats = fs.statSync(dir + filesArray[i]); else if (stats.isFile()) { files1.push(filesArray[i]); } } console.log(dir1 , files1 + " return") return dir1, files1 } dir1, files1 = file_tree(dir1, files1) console.log("!!!dir1: ", dir1) console.log("!!!files1: ", files1); for (let i = 0; i < dir1.length; i++) { console.log("dir: " + dir1[i]) } for (let i = 0; i < files1.length; i++) { console.log("file: " + files1[i]) }` It works but files1 is stored in objekt – Test Ware Aug 25 '23 at 17:07
  • Don't try to put long code in comments. Edit the question to show what you tried. – Barmar Aug 25 '23 at 17:08
  • You seem to have `else if` without an `if` before it. – Barmar Aug 25 '23 at 17:08
  • Yes, I cut "if" out to save space – Test Ware Aug 25 '23 at 17:10
  • I can't read that unformatted code. Please EDIT THE QUESTION. – Barmar Aug 25 '23 at 17:13
  • The key is Promise although I don't have a Promise in the code – Test Ware Aug 25 '23 at 17:14
  • You can't return multiple variables in JavaScript. `return dir1, files1` is equivalent to `return files1` – Barmar Aug 25 '23 at 17:14
  • You would get a Promise if you write `async function`. – Barmar Aug 25 '23 at 17:15
  • If you want to return multiple variables, use destructuring: `return [variable1, variable2]` in the function, and `[var1, var2] = functionName(...)` when calling. – Barmar Aug 25 '23 at 17:16
  • `function file_tree{ const filesArray = fs.readdirSync(dir) for() {let stats = fs.statSync(dir + filesArray[i]); if (stats.isFile()) {files1.push(filesArray[i]);} } return dir1, files1} dir1, files1 = file_tree(dir1, files1) console.log("!!!files1: ", files1); for (){console.log("file: " + files1[i])}` – Test Ware Aug 25 '23 at 17:21
  • I'm ignoring you until you edit the question. – Barmar Aug 25 '23 at 17:24
  • I'm going to lunch now, so you have an hour to do it right. – Barmar Aug 25 '23 at 17:24
  • finished, thanks for the help – Test Ware Aug 25 '23 at 17:28
  • I don't know how to display the code better because spaces are deleted and a character limit – Test Ware Aug 25 '23 at 17:30
  • See how I formatted your original code in edit #2. Paste your code and put 3 backticks on the lines before and after. – Barmar Aug 25 '23 at 18:41
  • what are backticks? – Test Ware Aug 26 '23 at 14:19
  • This character: `\`` On US keyboards it's on the same key as tilde `~`. – Barmar Aug 26 '23 at 17:41
  • Another option is to mark your code and then click in the `{}` in the toolbar. See https://stackoverflow.com/help/formatting – Barmar Aug 26 '23 at 17:42
  • I used backticks!? – Test Ware Aug 27 '23 at 17:19
  • Sorry, I misread my edit. I just cleaned up the indentation and excess spacing that made it hard to read. So if you know how to post code, why don't you post the updated code that's still not working? – Barmar Aug 28 '23 at 02:26

0 Answers0