最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
node.js与C语言 实现遍历文件夹下最大的文件,并输出路径,大小
时间:2017-02-09 编辑:简简单单 来源:一聚教程网
node.js版
遍历文件夹下最大的文件,并输出路径,大小
实现代码:
代码如下 | 复制代码 |
/* 遍历文件夹下最大的文件,并输出路径,大小 */
functionfindmax(basepath){ //只能执行一次 if(findmax.s)return; findmax.s =true; varfs = require('fs'); varmaxfile = 0; varcount = 0; varbegin =newDate().getTime(); functionTraversal(filepath){ fs.readdir(filepath,function(err,files){ if(err)return; files.forEach(function(file,index,files){ //console.log(index + "=" + filepath +"\\" + file); vartmppath = filepath +"\\"+ file; fs.stat(tmppath,function(err, stats) { if(err) { console.log("打开文件错误"+ err); return; }; if(stats.isDirectory()) Traversal(tmppath); else{ //console.log(++count +" "+ tmppath + " " + stats.size); count++; if(maxfile < stats.size) maxfile = stats.size; } }); }); }); } Traversal(basepath); process.on('exit',function() { varend =newDate().getTime(); console.log(count +'结束耗时:'+ (end - begin) +"ms "+ maxfile); }); console.log(basepath); }
findmax('D:\\devtools\\'); |
C/C++实现代码
代码如下 | 复制代码 |
#include #include #include
DWORDmaxsize = 0; clock_tstart, end; DWORDcount = 0;
voidfind(char* lpPath) { charszFind[MAX_PATH],szFile[MAX_PATH]; DWORDtmpsize = 0; WIN32_FIND_DATA FindFileData; strcpy(szFind,lpPath); strcat(szFind,"\\*.*"); HANDLEhFind=FindFirstFile(szFind,&FindFileData); if(INVALID_HANDLE_VALUE == hFind)return; while(TRUE) { if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//如果为目录 { if(FindFileData.cFileName[0] !='.')//判断是否为. or .. { strcpy(szFile,lpPath); strcat(szFile,"\\"); strcat(szFile,FindFileData.cFileName); find(szFile);//递归调用 } }else{ //printf("%s\n",FindFileData.cFileName); count++;//文件计数 tmpsize = FindFileData.nFileSizeLow; if(maxsize < tmpsize) maxsize = tmpsize; } //下一个文件为空,则退出 if(!FindNextFile(hFind,&FindFileData))break; } }
voidmain() { charfilepath[MAX_PATH]="d:\\devtools"; printf("%s\n",filepath); start =clock(); find(filepath); end =clock(); printf("文件数:%d %dms max File:%d",count,end - start,maxsize); //system("PAUSE"); } |
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31