MFC recursive file search function code

Source: Internet
Author: User

I wrote a function for searching files under MFC, recursively traversing all levels of sub-directories for search, not case sensitive. Recursion is a good thing. The code written by recursion is usually short, but not easy to understand and error-prone. Pay more attention to it. After all, this function is just a little thing with less than 30 lines of code. Disadvantages: wildcards *?. Improvements will be made in the future. Call example: CStringArray aStrFile; int I = CFindFile (_ T ("D: \"), _ T ("qq.txt"), & aStrFile); function code: [cpp] // The first parameter is the path to be searched, such as "c: \ dir \" or "c: \ dir "searches for the file whose second parameter is the file to be searched in all subdirectories including this directory, for example," qq.txt ". The file suffix must be provided, case Insensitive // The third parameter is used to save the full path of the searched file. Construct a CStringArray object and input the address INT CFindFile (const TCHAR * szPath, const TCHAR * szFileName, CStringArray * paStrResult) {ASSERT (paStrResult! = NULL); CString StrPath (szPath); CString StrFileName (szFileName); // check whether the last character of the path string is '\'. if not, add if (StrPath [StrPath. getLength ()-1]! = _ T ('\') StrPath + = _ T ('\'); CString StrFindPath = StrPath + _ T ("*. * "); // start searching for CFileFind finder; BOOL bWorking = finder. findFile (StrFindPath); while (bWorking) {bWorking = finder. findNextFile (); // if a folder is found, recursively go to if (finder. isDirectory ()&&! Finder. isDots () {CFindFile (StrPath + finder. getFileName (), StrFileName, paStrResult);} // if the file is found, add the absolute path to the string array. The case sensitivity is not if (finder. getFileName (). compareNoCase (StrFileName) = 0) {paStrResult-> Add (finder. getFilePath () ;}/// returns the number of objects found. INT nFile = paStrResult-> GetSize (); return nFile ;}

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.