一直关注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 })
 posted on 2012-01-04 19:22  落叶满长沙  阅读(13976)  评论(0编辑  收藏  举报