Node. js and C can traverse the largest file in the folder and output the path, size, and node. js path.
Node. js
Traverse the maximum file in the folder and output the path, size
Implementation Code:
/* Traverse the maximum file in the folder and output the path. */function findmax (basepath) {// only one 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 ("file opening error" + 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 time consumed: '+ (end-begin) + "ms" + maxfile) ;}); console. log (basepath);} findmax ('d: \ devtools \\');
C/C ++ implementation code
# 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. dwFil EAttributes & FILE_ATTRIBUTE_DIRECTORY) // if the directory is {if (FindFileData. cFileName [0]! = '. ') // Determines whether it is. or .. {strcpy (szFile, lpPath); strcat (szFile, "\"); strcat (szFile, FindFileData. cFileName); find (szFile); // recursive call} else {// printf ("% s \ n", FindFileData. cFileName); count ++; // file count tmpsize = FindFileData. nFileSizeLow; if (maxsize <tmpsize) maxsize = tmpsize;} // exit if (! FindNextFile (hFind, & FindFileData) break ;}} void main () {char filepath [MAX_PATH] = "d: \ devtools"; printf ("% s \ n ", filepath); start = clock (); find (filepath); end = clock (); printf ("number of files: % d % dms max File: % d", count, end-start, maxsize); // system ("PAUSE ");}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!