Implementing file Retrieval under Windows

Source: Internet
Author: User

Recursive functions are most suitable for applications where file retrieval is possible. Windows system provides FindFirstFile and FindNextFile two system calls, using these two functions, we can easily implement the file retrieval under Windows.

Source Files <demo.cpp>

#include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <string> #include < iostream>using namespace Std;int num = 0;void searchfile (wstring filePath);//file Retrieval function int main () {     wstring path = L "C:";//Set the root directory     searchfile (path);//Search Directory     printf ("Total file count:%d\n", num);     system ("pause");} /** Retrieving file Functions * @param folder path */void searchfile (wstring filePath) {        //Define a structure, For storing system call return function     win32_find_data findData = {0};    //findfirstfile function returns a handle, The results are deposited in findData     handle HANDLE =: FindFirstFile ((filepath+l "\\*.*"). C_str (), &finddata);     //to determine if the handle is valid     if (invalid_handle_value = = HANDLE)     {   & nbsp;    return;    }    else    {         do       &NBSP;{&NBSP;&NBsp          if (finddata.cfilename[0] = = L '. ') Ignore current directory (.) And up-level directories (.. )                 continue;             num++;//file count plus one                         if (finddata.dwfileattributes&file_attribute_directory)//The file is a directory file             {                 wstring TempFilePath = filePath + L "\ \" + finddata.cfilename;        & nbsp;       searchfile (TempFilePath);//recursive call, retrieving subdirectories              }            if (finddata.dwfileattributes&file _attribute_normal)//The file is a normal file             { &nbsP              wcout << filePath + L "\ \" + Finddata.cfilename < < endl;            }        } while ( FindNextFile (handle, &finddata));//Find the next file in the current directory     }    ::findclose (handle);// Close the Find handle}

Note: Wstring,wcout usage and string,cout are the same, except that all operands become wide characters.

Implementing file Retrieval under Windows

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.