C++文件搜索

来源:互联网 发布:最便宜的阿里云 编辑:程序博客网 时间:2024/06/04 17:40

//支持子目录,隐藏文件,只读文件的查找
//使用方法:用命令行输入。例如,程序名为Find.exe,则命令行为 Find yourfile
//查找支持通配符*,? 

//支持命令行

#define _WIN32_WINNT 0x0400

#include <iostream>
#include <stdlib.h>
using namespace std;

#ifdef UNICODE
#undef UNICODE
#endif

#include <windows.h>

BOOL MyFindFile(LPCSTR sFindPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
    char sPath[MAX_PATH];
    char sFormatFileName[MAX_PATH+2] = "*";
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    BOOL fFinished = FALSE;

    lstrcpy(sFormatFileName, sFindPath);
    lstrcat(sFormatFileName, "//*");
    lstrcat(sFormatFileName, sFindFileName);
    lstrcat(sFormatFileName, "*");

    hFind = FindFirstFile(sFormatFileName, &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) 
    {
        return FALSE;
    }
    else 
    {
        while (!fFinished) 
        {
            lstrcpy(sPath, sFindPath);
            lstrcat(sPath, "//");
            lstrcat(sPath, FindFileData.cFileName);

            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
            {
                if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
                    cout << "  Folder " << ++uCountFolder << ". - " << sPath <<""<< endl;
            }
            else
                cout << "  File   " << ++uCountFile << ". - " << sPath <<""<< endl;

            if (!FindNextFile(hFind, &FindFileData)) 
            {
                if (GetLastError() == ERROR_NO_MORE_FILES) 
                {
                    fFinished = TRUE; 
                } 
                else 
                {
                    break;
                } 
            }
        }

        FindClose(hFind);
    }

    return TRUE;
}

BOOL MyFindFolder(LPCSTR sPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
    char sTemp[MAX_PATH];
    char sFormatFileName[MAX_PATH];
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    BOOL fFinished = FALSE;

    MyFindFile(sPath, sFindFileName, uCountFolder, uCountFile);

    lstrcpy(sFormatFileName, sPath);
    lstrcat(sFormatFileName, "//*");
    hFind = FindFirstFile(sFormatFileName, &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) 
    {
        return FALSE;
    }
    else 
    {
        while (!fFinished) 
        {
            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
            {
                if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
                {
                    lstrcpy(sTemp, sPath);
                    lstrcat(sTemp, "//");
                    lstrcat(sTemp, FindFileData.cFileName);
                    MyFindFolder(sTemp, sFindFileName, uCountFolder, uCountFile);
                }
            }

            if (!FindNextFile(hFind, &FindFileData)) 
            {
                if (GetLastError() == ERROR_NO_MORE_FILES) 
                {
                    return TRUE;
                } 
                else 
                { 
                    return FALSE;
                } 
            }
        }

        FindClose(hFind);
    }

    return TRUE;
}

void GetFileName(LPCSTR sFullPath, LPSTR sFilePath, LPSTR sFileName)
{
    LPSTR p = (LPSTR)(sFullPath + lstrlen(sFullPath) - 1);
    bool Flag(false);

    while (p != sFullPath)
    {
        if ('//' == *p || '/' == *p)
        {
            Flag = true;
            break;
        }
        p--;
    }

    if (Flag)
    {
        lstrcpy(sFileName, p + 1);
        lstrcpy(sFilePath, sFullPath);
        sFilePath[p-sFullPath] = '/0';
    }
    else
    {
        lstrcpy(sFileName, sFullPath);
        GetFullPathName(".", MAX_PATH, sFilePath, NULL);
    }
}

ULONGLONG MyStartFind(LPCSTR sFindFileName)
{
    char sPath[MAX_PATH];
    char sFileName[MAX_PATH];
    ULONGLONG    uCountFolder(0);
    ULONGLONG    uCountFile(0);
    
    GetFileName(sFindFileName, sPath, sFileName);
    MyFindFolder(sPath, sFileName, uCountFolder, uCountFile);

    if (uCountFolder + uCountFile)
    {
        cout << "------------------------------------------------------------"<< endl; 
        cout << "Total Folders:"<< uCountFolder << "" << endl; 
        cout << "Total Files:  "<< uCountFile << "" << endl; 
    }
    else
        cout << "Couldn't Find File." << endl;

    return uCountFolder + uCountFile;
}

int main(int argc, char *argv[])
{
    char sFindFileName[MAX_PATH];
    
    if (argc < 2)
    {
        cout << "Enter Find File Name:";
        cin >> sFindFileName;
    }
    else
    {
        lstrcpy(sFindFileName, argv[1]);
    }
    MyStartFind(sFindFileName);

#ifdef _DEBUG
    system("pause");
#else
    if (argc < 2)
        system("pause");
#endif

    return (0);
}

/*Output:
C:/a>find a
    File   1. - C:/a/a.mdb
    File   2. - C:/a/a.txt
    Folder 1. - C:/a/a1
    File   3. - C:/a/a1/a1.mdb
    File   4. - C:/a/a1/a1.txt
    Folder 2. - C:/a/a1/a2
    File   5. - C:/a/a1/a2/a2.mdb
    File   6. - C:/a/a1/a2/a2.txt
--------------------------------
Total Folders:2
Total Files:  6
*/

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qmroom/archive/2008/08/19/2794317.aspx

原创粉丝点击