0

I have a nested object tree which represents a folder structure. I need to build multiple directories by traversing through the nodes.

diagram of folder structure:

enter image description here

Here's my folder structure's object: https://jsfiddle.net/himel023/8jw3yhs0/

I have tried recursive and non-recursive approach. Not working the way i wanted it to.

Here's my jsfiddle link: https://jsfiddle.net/himel023/sr25xq8L/

some sample code that i have tried:

let crDirList = (tree) => {
  for (let i = 0; i < tree.length; i++) {
    let tempSingleDir = crSingleDir(tree[i]);
    console.log(tempSingleDir);
    let curDir = tree[i];

    while (curDir.hasChild) {

      curDir = curDir.subFolder.folders;
      crDirList(curDir);

    }
  }

}

let dirArr = [];

function crSingleDir (tree) {
  let dirToBuildArr = [];

  let subf = tree.subFolder.folders;
  if (tree.hasChild) {
    for (let i = 0; i < subf.length; i++) {
      dirToBuildArr[i] = `/${tree.name}/${subf[i].name}`;
    }
  } else {
    dirToBuildArr[0] = 0;
  }
  return(dirToBuildArr);
}

let fst = treeObject.folders;
crDirList(fst.folders);

I am getting only one level of subfolders for e.g. "/video/Synopi-Videos" or "/Other videos/Urgent folder".

But what i am trying to do is getting some outputs like "/video/Synopi-Videos/Empty Folders" , then again "/video/Synopi-Videos/Other Videos/Urget folder/" etc.

James Z
  • 12,209
  • 10
  • 24
  • 44
Kaisar
  • 53
  • 5
  • I was just doing something similar. Have a look at [31645738](https://stackoverflow.com/questions/31645738/how-to-create-full-path-with-nodes-fs-mkdirsync). This related post helped me. – RichS May 07 '19 at 06:17
  • 1
    Any particular reason you're using that folder structure? There's a lot of things that could be done to make it a little simpler. For instance, `hasChild` is redundant. If `subFolders` exists `hasChild` should be true. Adds complexity but no value – Tibrogargan May 07 '19 at 06:19
  • @RichS I have written the nodejs api for creating a directory if it already does not exist (link: https://jsfiddle.net/himel023/ypj4otrs/ ). But the problem i am having is to deriving the directroy structure from object tree, that is needed to pass to that function. – Kaisar May 07 '19 at 06:32
  • @Tibrogargan You are right. But I thought it would be cleaner for handling codes and stuffs. Rather i am building a single page application so i had to manually assign some ids to the folder which i did not like as well. But i did not find any better workaround. – Kaisar May 07 '19 at 06:38

0 Answers0