node.js递归打印文件目录、文件名

var fs = require('fs');
var sep = require('path').sep;

/**
 * 递归打印文件目录、文件名
 */
var readDir = function(path) {
    var exists = fs.existsSync(path),
        stat = fs.statSync(path);

    if (exists && stat) { //判断文件、文件目录是否存在
        if (stat.isFile()) {
            var fpath = path.split(sep);
            console.info(symbol(fpath) + fpath[fpath.length - 1]);
        } else if (stat.isDirectory()) {
            var fpath = path.split(sep);
            console.info(symbol(fpath) + fpath[fpath.length - 1]);
            var files = fs.readdirSync(path);
            if (files && files.length > 0) {
                files.forEach(function(file) {
                    readDir(path + sep + file); //递归
                });
            }
        }
    } else {
        console.info('根目录不存在.');
    }
};

//符号拼接。
//为了省事,这里采用了字符串拼接的方式,最优的方式应该用buffer来做字符拼接。
var symbol = function(fpath) {
    var s = ' - ';
    for (var i = 1; i < fpath.length; i++) {
        s += ' - ';
    }
    return s;
};

readDir('node_modules/ccap');

样例:


/usr/local/bin/node test.js
 -  - ccap
 -  -  - .npmignore
 -  -  - .travis.yml
 -  -  - addon
 -  -  -  - cap.cc
 -  -  -  - cap.h
 -  -  -  - hcaptha.cc
 -  -  -  - jpeglib
 -  -  -  -  - CImg.h
 -  -  -  -  - Licence_CeCILL-C_V1-en.txt
 -  -  -  -  - Licence_CeCILL_V2-en.txt
 -  -  -  -  - README.txt
 -  -  -  -  - cderror.h
 -  -  -  -  - cdjpeg.h
 -  -  -  -  - jaricom.c
 -  -  -  -  - jcapimin.c
 -  -  -  -  - jcapistd.c
 -  -  -  -  - jcarith.c
 -  -  -  -  - jccoefct.c
 -  -  -  -  - jccolor.c
 -  -  -  -  - jcdctmgr.c
 -  -  -  -  - jchuff.c
 -  -  -  -  - jcinit.c
 -  -  -  -  - jcmainct.c
 -  -  -  -  - jcmarker.c
 -  -  -  -  - jcmaster.c
 -  -  -  -  - jcomapi.c
 -  -  -  -  - jconfig.h
 -  -  -  -  - jcparam.c
 -  -  -  -  - jcprepct.c
 -  -  -  -  - jcsample.c
 -  -  -  -  - jctrans.c
 -  -  -  -  - jdapimin.c
 -  -  -  -  - jdapistd.c
 -  -  -  -  - jdarith.c
 -  -  -  -  - jdatadst.c
 -  -  -  -  - jdatasrc.c
 -  -  -  -  - jdcoefct.c
 -  -  -  -  - jdcolor.c
 -  -  -  -  - jdct.h
 -  -  -  -  - jddctmgr.c
 -  -  -  -  - jdhuff.c
 -  -  -  -  - jdinput.c
 -  -  -  -  - jdmainct.c
 -  -  -  -  - jdmarker.c
 -  -  -  -  - jdmaster.c
 -  -  -  -  - jdmerge.c
 -  -  -  -  - jdpostct.c
 -  -  -  -  - jdsample.c
 -  -  -  -  - jdtrans.c
 -  -  -  -  - jerror.c
 -  -  -  -  - jerror.h
 -  -  -  -  - jfdctflt.c
 -  -  -  -  - jfdctfst.c
 -  -  -  -  - jfdctint.c
 -  -  -  -  - jidctflt.c
 -  -  -  -  - jidctfst.c
 -  -  -  -  - jidctint.c
 -  -  -  -  - jinclude.h
 -  -  -  -  - jmemmgr.c
 -  -  -  -  - jmemnobs.c
 -  -  -  -  - jmemsys.h
 -  -  -  -  - jmorecfg.h
 -  -  -  -  - jpegint.h
 -  -  -  -  - jpeglib.h
 -  -  -  -  - jquant1.c
 -  -  -  -  - jquant2.c
 -  -  -  -  - jutils.c
 -  -  -  -  - jversion.h
 -  -  -  -  - libjpeg.so
 -  -  -  -  - plugins
 -  -  -  -  -  - add_fileformat.h
 -  -  -  -  -  - cimgIPL.h
 -  -  -  -  -  - cimg_ipl.h
 -  -  -  -  -  - cimgmatlab.h
 -  -  -  -  -  - draw_gradient.h
 -  -  -  -  -  - jpeg_buffer.h
 -  -  -  -  -  - loop_macros.h
 -  -  -  -  -  - nlmeans.h
 -  -  -  -  -  - skeleton.h
 -  -  -  -  -  - vrml.h
 -  -  -  -  -  - vtk.h
 -  -  -  -  - resources
 -  -  -  -  -  - compile_win_icl.bat
 -  -  -  -  -  - compile_win_visualcpp.bat
 -  -  -  -  - transupp.h
 -  -  -  - libjpeg.dll
 -  -  - benchmark
 -  -  -  - generate.js
 -  -  - binding.gyp
 -  -  - build
 -  -  -  - Makefile
 -  -  -  - Release
 -  -  -  -  - .deps
 -  -  -  -  -  - Release
 -  -  -  -  -  -  - hcaptha.node.d
 -  -  -  -  -  -  - obj.target
 -  -  -  -  -  -  -  - hcaptha
 -  -  -  -  -  -  -  -  - addon
 -  -  -  -  -  -  -  -  -  - cap.o.d
 -  -  -  -  -  -  -  -  -  - hcaptha.o.d
 -  -  -  -  - hcaptha.node
 -  -  -  -  - obj.target
 -  -  -  -  -  - hcaptha
 -  -  -  -  -  -  - addon
 -  -  -  -  -  -  -  - cap.o
 -  -  -  -  -  -  -  - hcaptha.o
 -  -  -  - binding.Makefile
 -  -  -  - config.gypi
 -  -  -  - gyp-mac-tool
 -  -  -  - hcaptha.target.mk
 -  -  - cap_img
 -  -  - example
 -  -  -  - custom_get.js
 -  -  -  - custom_get2.js
 -  -  -  - default_get.js
 -  -  -  - web_example.js
 -  -  - index.js
 -  -  - lib
 -  -  -  - dir.js
 -  -  -  - gen.js
 -  -  -  - hcap.js
 -  -  -  - timer.js
 -  -  - make.js
 -  -  - node_modules
 -  -  -  - nan
 -  -  -  -  - .dntrc
 -  -  -  -  - CHANGELOG.md
 -  -  -  -  - LICENSE.md
 -  -  -  -  - appveyor.yml
 -  -  -  -  - include_dirs.js
 -  -  -  -  - nan.h
 -  -  -  -  - nan_implementation_12_inl.h
 -  -  -  -  - nan_implementation_pre_12_inl.h
 -  -  -  -  - nan_new.h
 -  -  -  -  - nan_string_bytes.h
 -  -  -  -  - package.json
 -  -  -  - node-schedule
 -  -  -  -  - .npmignore
 -  -  -  -  - README.md
 -  -  -  -  - lib
 -  -  -  -  -  - increment.js
 -  -  -  -  -  - schedule.js
 -  -  -  -  - package.json
 -  -  -  -  - test
 -  -  -  -  -  - convenience-method-test.js
 -  -  -  -  -  - date-convenience-methods-test.js
 -  -  -  -  -  - job-test.js
 -  -  -  -  -  - range-test.js
 -  -  -  -  -  - recurrence-rule-test.js
 -  -  - package.json
 -  -  - readme.md
 -  -  - test
 -  -  -  - main_test.js


Process finished with exit code 0


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值