Count all computer files

Source: Internet
Author: User

It's okay to write and play, that is, to be familiar with the API. There's no technology at all... [cpp] # include <windows. h >#include <algorithm> # include <iostream> # include <iomanip> # include <fstream> # include <string> using namespace std; static int count = 0; // record the number of files void FindInAll (string & Path, fstream & outfile) {string szFind; szFind = Path; szFind + = "*. * "; WIN32_FIND_DATA FindFileData; // The WIN32_FIND_DATA structure contains all the information about the file. HANDLE hFind = FindFirstFile (szFind. c_str (),& FindFileData); // FindFirstFile find the file based on the file name if (hFind = INVALID_HANDLE_VALUE) return; do {if (FindFileData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // If a directory is found, enter this directory for recursion {string szFile; // remove the directory. and .. directory szFile = Path + FindFileData. cFileName + "\"; string test = szFile. substr (szFile. length ()-3, szFile. length (); if (test = "\\. \ "| test = ".. \ ") continue; FindInAll (szFile, outfile);} else // find the file {string sz File; szFile = Path + FindFileData. cFileName; outfile <szFile <endl;: count =: count + 1 ;}} while (FindNextFile (hFind, & FindFileData); FindClose (hFind );} int main () {// used to record the clock_t start, finish; double totaltime; start = clock (); // start cout <"******************************** * ************************** "<endl; cout <"--------------- Created By Cryking 2012.11.02 ---------------" <endl; cout <"this program Automatically traverse all files on the computer (including hidden files) for a long time. Please wait... "<endl; cout <"************************************ * ********************** "<endl; fstream outfile ("allfile.txt", ios: out); cout <"---------------- start by traversing all files on the disk: ---------------------" <endl; outfile <setw (3) <"------ traverse all files on the disk start: -----------" <endl; // traverse the entire computer's disk int DSLength = GetLogicalDriveStrings (0, NULL ); // obtain the total drive length char * Path = new char [DSLength]; GetLogicalDriveStrin Gs (DSLength, Path); // get the first drive address while (Path! = NULL) {// directly traverse all files on drive d if (string) Path = "") // The Path has a break error; // jump out of the loop cout <"traversing" <Path <"disk file, please wait... "<endl; FindInAll (string) Path, outfile); Path = Path + strlen (Path) + 1; // obtain the next drive address based on the current drive address} locale :: global (locale ("C"); finish = clock (); // time end totaltime = (double) (finish-start)/CLOCKS_PER_SEC; outfile <setw (3) <"------ traverse all files on the disk end -----------" <endl; outfile <setw (3) <"The Traversal Time: "<totaltime <" seconds "<endl; outfile <<Setw (3) <"Total number of files on the PC:" <: count <endl; outfile <setw (3) <"------------ traversal completed ---------------" <endl; outfile. close (); cout <"after all, please upload the allfile.txt file under this directory! "<Endl; system (" pause "); return 0;} the running time is not as slow as I thought. recursion is used, in addition, all the file path information is written to the file (hey, it seems like it can be used to do some bad things ·). After running, the file size is about 30 M. If it is opened using notepad, it is a little slow, we recommend that you use UE to open it and check my results.

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.