node.js与C语言 实现遍历文件夹下最大的文件,并输出路径,大小
2019/6/27 21:27:58
本文主要是介绍node.js与C语言 实现遍历文件夹下最大的文件,并输出路径,大小,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
node.js版
遍历文件夹下最大的文件,并输出路径,大小
实现代码:
/* 遍历文件夹下最大的文件,并输出路径,大小 */ function findmax(basepath){ //只能执行一次 if(findmax.s) return; findmax.s = true; var fs = require('fs'); var maxfile = 0; var count = 0; var begin = new Date().getTime(); function Traversal(filepath){ fs.readdir(filepath, function(err,files){ if(err) return; files.forEach(function(file,index,files){ //console.log(index + "=" + filepath +"\\" + file); var tmppath = 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 () { var end = new Date().getTime(); console.log(count + '结束耗时:' + (end - begin) + "ms " + maxfile); }); console.log(basepath); } findmax('D:\\devtools\\');
C/C++实现代码
#include <stdio.h> #include <windows.h> #include <time.h> DWORD maxsize = 0; clock_t start, end; DWORD count = 0; void find(char * lpPath) { char szFind[MAX_PATH],szFile[MAX_PATH]; DWORD tmpsize = 0; WIN32_FIND_DATA FindFileData; strcpy(szFind,lpPath); strcat(szFind,"\\*.*"); HANDLE hFind=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; } } void main() { char filepath[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"); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇关于node.js与C语言 实现遍历文件夹下最大的文件,并输出路径,大小的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-21Vue3教程:新手入门与实战指南
- 2024-09-21VueRouter4教程:入门与实践指南
- 2024-09-21Vuex4项目实战:新手入门教程
- 2024-09-21Vue3项目实战:初学者指南
- 2024-09-21Vue3项目实战:从零开始构建你的第一个Vue3应用
- 2024-09-21VueRouter4项目实战入门教程
- 2024-09-21Vue3入门:新手必读指南
- 2024-09-20Vue3教程:初学者快速入门指南
- 2024-09-20Vue资料入门教程:从零开始学习Vue.js
- 2024-09-20Vuex4入门教程:轻松掌握Vue状态管理