您的位置:首页 > Web前端 > Node.js

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

2016-03-16 23:57 776 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: