zoukankan      html  css  js  c++  java
  • nodejs 遍历文件夹 获取 文件夹下所有文件列表

    一直关注node.js的发展,但是没有动手写过东西,前面同事帮忙用python写了个工具,

    获取一个文件夹下面的所有文件名的列表,python真的是强大,俺决定用node来写一个。

    使用方法,把下面代码保存为一个js文件比如xxoo.js ,然后打开命令行工具,进入xxoo.js

    所在目录,输入:

    node xxoo.js '这里为你要统计的目标文件夹的目录'

    然后 你会 在 xxoo.js所在目录,发现一个res.lst的文件名,这个文件名 由你自己决定,见

    代码第三行,这个文件里面就包含了你要统计目录的所有子文件列表。

     1 var fs = require('fs');
    2 var root_path=process.argv[2];
    3 var w_file='res.lst';
    4 function getAllFiles(root){
    5 var res = [] , files = fs.readdirSync(root);
    6 files.forEach(function(file){
    7 var pathname = root+'/'+file
    8 , stat = fs.lstatSync(pathname);
    9
    10 if (!stat.isDirectory()){
    11 res.push(pathname.replace(root_path,'.'));
    12 } else {
    13 res = res.concat(getAllFiles(pathname));
    14 }
    15 });
    16 return res
    17 }
    18 var w_content=getAllFiles(root_path).join('\n');
    19 fs.readFile(root_path+w_file,function(err , data){
    20 if(err && err.errno==33){
    21 fs.open(w_file,"w",0666,function(e,fd){
    22 if(e) throw e;
    23 fs.write(fd,w_content,0,'utf8',function(e){
    24 if(e) throw e;
    25 fs.closeSync(fd);
    26 })
    27 });
    28 } else{
    29 fs.writeFile(root_path+w_file,w_content,function(e){
    30 if(e) throw e
    31 })
    32 }
    33 })
  • 相关阅读:
    R中character和factor的as.integer的不同
    ggplot2练习
    R的plotmath
    Python数据科学手册(2) NumPy入门
    Python数据科学手册(1) IPython:超越Python
    ggplot2(6) 标度、坐标轴和图例
    R自带数据集
    ggplot2(5) 工具箱
    MATLAB神经网络(7) RBF网络的回归——非线性函数回归的实现
    ggplot2(4) 用图层构建图像
  • 原文地址:https://www.cnblogs.com/litao229/p/2312393.html
Copyright © 2011-2022 走看看