且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Node.js将文件系统中的目录结构转换为JSON

更新时间:2023-02-25 15:13:14

这是草图。错误处理作为读者的练习留下。

  var fs = require('fs'),
path = require('path')

函数dirTree(filename){
var stats = fs.lstatSync(filename),
info = {
path:filename,
name:path.basename(filename)
};

if(stats.isDirectory()){
info.type =folder;
info.children = fs.readdirSync(filename).map(function(child){
return dirTree(filename +'/'+ child);
});
} else {
//假设它是一个文件。在现实生活中,它可能是一个符号链接或
//其他的东西!
info.type =file;
}

返回信息;
}

if(module.parent == undefined){
// node dirTree.js〜/ foo / bar
var util = require('util' );
console.log(util.inspect(dirTree(process.argv [2]),false,null));
}


I have a file structure like this:

root
|_ fruits
|___ apple
|______images
|________ apple001.jpg
|________ apple002.jpg
|_ animals
|___ cat
|______images
|________ cat001.jpg
|________ cat002.jpg

I would like to, using Javascript and Node.js, listen to this root directory and all sub directories and create a JSON which mirrors this directory structure, each node contains type, name, path, and children:

data = [
  {
    type: "folder",
    name: "animals",
    path: "/animals",
    children: [
      {
        type: "folder",
        name: "cat",
        path: "/animals/cat",
        children: [
          {
            type: "folder",
            name: "images",
            path: "/animals/cat/images",
            children: [
              {
                type: "file",
                name: "cat001.jpg",
                path: "/animals/cat/images/cat001.jpg"
              }, {
                type: "file",
                name: "cat001.jpg",
                path: "/animals/cat/images/cat002.jpg"
              }
            ]
          }
        ]
      }
    ]
  }
];

Here's a coffeescript JSON:

data = 
[
  type: "folder"
  name: "animals"
  path: "/animals"
  children  :
    [
      type: "folder"
      name: "cat"
      path: "/animals/cat"
      children:
        [
          type: "folder"
          name: "images"
          path: "/animals/cat/images"
          children: 
            [
              type: "file"
              name: "cat001.jpg"
              path: "/animals/cat/images/cat001.jpg"
            , 
              type: "file"
              name: "cat001.jpg"
              path: "/animals/cat/images/cat002.jpg"
            ]
        ]
    ]
]

how to get this json data format in django views?(python)

Here's a sketch. Error handling is left as an exercise for the reader.

var fs = require('fs'),
    path = require('path')

function dirTree(filename) {
    var stats = fs.lstatSync(filename),
        info = {
            path: filename,
            name: path.basename(filename)
        };

    if (stats.isDirectory()) {
        info.type = "folder";
        info.children = fs.readdirSync(filename).map(function(child) {
            return dirTree(filename + '/' + child);
        });
    } else {
        // Assuming it's a file. In real life it could be a symlink or
        // something else!
        info.type = "file";
    }

    return info;
}

if (module.parent == undefined) {
    // node dirTree.js ~/foo/bar
    var util = require('util');
    console.log(util.inspect(dirTree(process.argv[2]), false, null));
}