Node. js and C can traverse the largest file in the folder and output the path, size, and node. js path.

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.