List all files on a disk

Source: Internet
Author: User

FileObject. h

 

# Pragma once <br/> # include <string> <br/> # include <vector> <br/> using namespace std; <br/> # ifdef _ UNICODE <br/> # define stdTstring wstring <br/> # else <br/> # define stdTstring <br/> # endif <br/> typedef enum <br/>{< br/> FILETYPE_NULL = 0, <br/> FILETYPE_FILE, <br/> FILETYPE_DIR, <br/>} FILETYPE; <br/> # define attributes <br/> # define FILEATTRIBUTE_READONLY0x00000001 <br/> # define FIL EATTRIBUTE_HIDE0x00000010 <br/> # define FILEATTRIBUTE_DIRECTORY0x00000100 <br/> class CFileObject <br/>{< br/> public: <br/> virtual ~ CFileObject (void); <br/> CFileObject (FILETYPE type, stdTstring FileName); <br/> private: <br/> stdTstring mFileName; <br/> FILETYPE mFileType; <br/> public: <br/> void SetFileName (stdTstring name); <br/> stdTstring GetFileName (void); <br/> FILETYPE GetFileType (); <br/> vector <CFileObject *> mSubFiles; <br/> };

FileObject. cpp

# Include "StdAfx. h "<br/> # include" FileObject. h "<br/> CFileObject: CFileObject (FILETYPE type, stdTstring FileName) <br/>: mFileName (FileName) <br/>, mFileType (type) <br/>{< br/> mSubFiles. clear (); <br/>}< br/> CFileObject ::~ CFileObject (void) <br/>{< br/> vector <CFileObject * >:: iterator iter = mSubFiles. begin (); <br/> for (; iter! = MSubFiles. end (); iter ++) <br/>{< br/> if (* iter )) <br/>{< br/> CFileObject * file = (* iter); <br/> delete file; <br/> file = NULL; <br/> * iter = NULL; <br/>}< br/> mSubFiles. clear (); <br/>}< br/> void CFileObject: SetFileName (stdTstring name) <br/>{< br/> mFileName = name; <br/>}< br/> stdTstring CFileObject: GetFileName (void) <br/>{< br/> return mFileName; <br/>}< br/> FILETYPE CFileObject: GetFileType () <br/>{< br/> return mFileType; <br/>}

 

Main. cpp

 

 

// Listfile. CPP: defines the entry point for the console application. <br/> // <br/> # include "stdafx. H "<br/> # include <windows. h> <br/> # include "fileobject. H "<br/> # include" tinyxml. H "<br/> # include <time. h> <br/> bool enumfiles (lpctstr dirname, cfileobject & root) <br/>{< br/> win32_find_data findfiledata; <br/> zeromemory (& findfiledata, sizeof (win32_find_data); <br/> handle hfind = invalid_handle_value; <Br/> stdtstring dir = stdtstring (dirname); <br/> If (dir [dir. Length ()-1]! = '//') <Br/>{< br/> dir = dir + _ T ("//"); <br/>}< br/> stdtstring find = dir + _ T ("*. * "); <br/> bool bfindresult = true; <br/> hfind =: findfirstfile (find. c_str (), & findfiledata); <br/> DWORD dwerror; <br/> If (hfind = invalid_handle_value) <br/>{< br/> return false; <br/>}< br/> else <br/>{< br/> while (bfindresult) <br/>{< br/> If (! _ Tcscmp (findfiledata. cfilename, _ T (".") |! _ Tcscmp (findfiledata. cfilename, _ T (".. ") <br/>{< br/> bfindresult =: findnextfile (hfind, & findfiledata); <br/> continue; <br/>}< br/> else <br/>{< br/> If (findfiledata. dwfileattributes & file_attribute_directory) <br/>{< br/> stdtstring fullpath = dir + stdtstring (findfiledata. cfilename); <br/> cfileobject * OBJ = new cfileobject (filetype_dir, fullpath); <br/> enumfiles (fullpath. c_str (), (* OBJ); <br/> roo T. msubfiles. push_back (OBJ); <br/>}< br/> else <br/>{< br/> stdtstring fullpath = dir + stdtstring (findfiledata. cfilename); <br/> cfileobject * OBJ = new cfileobject (filetype_file, fullpath); <br/> root. msubfiles. push_back (OBJ); <br/>}< br/> bfindresult =: findnextfile (hfind, & findfiledata ); <br/>}< br/> dwerror = getlasterror (); <br/>: findclose (hfind); <br/> If (dwerror! = Error_no_more_files) <br/>{< br/> _ tprintf (text ("findnextfile error. error is % u. /n "), <br/> dwerror); <br/> return false; <br/>}< br/> return true; <br/>}< br/> bool writetofile (cfileobject & root) <br/>{< br/> return true; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> time_t starttime; <br/> time (& starttime); <br/> DWORD star = gettickcount (); <br/> cfileobject OBJ (filetype_dir, _ T ("C: // "); <br/> enumfiles (_ T (" C: // "), OBJ); <br/> time_t endtime; <br/> time (& endtime); <br/> dword t = gettickcount ()-star; <br/> time_t deleta = endtime-starttime; <br/> return 0; <br/>}< br/>


 

In this way, it takes 8 seconds to traverse files on drive C after writing... seek a more efficient solution!

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.