Win32 C + + Traversal directory and File source code

Source: Internet
Author: User
Tags filetime

#include <windows.h>

#include <iostream>

#include <string>

using namespace Std;

only directories can be processed : Lppath can only be paths

void Find (char *lppath)

{

Char Szfind[max_path];

Char Szfile[max_path];

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]!= '. ')

{

strcpy (Szfile,lppath);

strcat (Szfile, "//");

strcat (Szfile,findfiledata.cfilename);

Find (Szfile);

}

}

Else

{

cout<<findfiledata.cfilename<<endl;

}

if (! FindNextFile (Hfind,&findfiledata))

Break

}

FindClose (hfind);

}

directories and files can be processed at the same time :p ath can be a path, a file name, or a file wildcard

void _find (string path) {

take the path name before the last "//" section , including "//"

String Prefix=path.substr (0,path.find_last_of ('//') +1);

Win32_find_data Findfiledata;

HANDLE Hfind=::findfirstfile (Path.c_str (), &findfiledata);

if (Invalid_handle_value = = hfind)

{

cout<< " file wildcard error " <<endl;

Return

}

while (TRUE)

{

Catalogue

if (Findfiledata.dwfileattributes & File_attribute_directory)

{

is not the current directory, nor is it a parent directory

if (findfiledata.cfilename[0]!= '. ')

{

Find Next Level directory

_find (prefix+findfiledata.cfilename+ "//" + "* *");

}

}

file

Else

{

cout<<findfiledata.cfilename<<endl;

}

if (! FindNextFile (Hfind,&findfiledata))

Break

}

FindClose (hfind);

}

void Main () {

Find ("E:");// directory:E - Drive

_find ("E://*.*");//e all files under disk

String str= "./test"; The directory is specified here

string path;

cout<< " Please enter a file wildcard:" <<flush;

cin>>path;

Str=str+path;

Find ((char*) str.c_str ());// can handle "." , ".."  can not handle "*" , ".. //*" 

_find (str);// can handle "*",".. * " cannot be processed ". " , ".."

System ("pause");

}

Copy Code

There is a struct, which is a C + + Self-contained, is this:

typedef struct _win32_find_data{

DWORD dwfileattributes;// file Properties

FILETIME ftcreationtime;// File creation Time

FILETIME ftlastaccesstime;// file last access time

FILETIME ftlastwritetime;// last modified time of file

DWORD Nfilesizehigh;// High file length + bit

DWORD nfilesizelow;// Low file length + bit

DWORD dwReserved0;// System Retention

DWORD dwReserved1;// System Retention

TCHAR cfilename[MAX_PATH];// Long File name

TCHAR calternatefilename[];// Optional name of the file

} win32_find_data;

Win32 C + + Traversal directory and File source code

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.