FindFirstFileÀ» ÀÌ¿ëÇØ ÆÄÀÏ ¸®½ºÆ® ±¸Çϱâ

ºñFindFirstFile À©µµ¿ì API¸¦ ÀÌ¿ëÇØ ÆÄÀÏ ¸®½ºÆ®¸¦  std::vector¿¡ ÀúÀåÇÑ´Ù.

pszFilterÀÇ ¿É¼Ç°ú iLevelÀÇ ¿É¼ÇÀº ¾Æ·¡ ¼Ò½ºÀÇ ÁÖ¼®¿¡¼­ Âü°í ÇÑ´Ù.

#include <stdio.h>

#include <windows.h>

#include <vector>

#include <string>

#include <iostream>

 

/*

pszDirectory : ã°íÀÚ ÇÏ´Â Æú´õÀÇ À§Ä¡

 

pszFilter : °Ë»öÇÒ ÇÊÅÍ

    *.dds  dds ÆÄÀϸ¸ °Ë»öÇÏ´Â °æ¿ì

    *      ¸ðµç ÆÄÀÏ¹× Æú´õ¸¦ ã´Â´Ù.

    \\    Æú´õ¸¸ ã´Â´Ù

 

iLevel    : °Ë»ö ÇÒ ÇÏÀ§ Æú´õÀÇ ±íÀÌ

    1      ¹Ù·Î ÇÏÀ§ Æú´õ¸¸ °Ë»ö

    -1    ¸ðµç Æú´õ °Ë»ö

    2      ¹Ù·Î ÇÏÀ§ Æú´õ, ±× ´ÙÀ½ ÇÏÀ§ Æú´õ °Ë»ö

*/

void GetFindFileList( char* pszDirectory, char* pszFilter, std::vector <std::string> *vtList, int iLevel = -1 )

{

    if( iLevel == 0 )

        return;

 

    char buf[MAX_PATH];

    WIN32_FIND_DATA FindFileData;

 

    bool bAddFile = true;

    bool bAddFolder = false;

    if( pszFilter[1] == 0 )

    {

        if( pszFilter[0] == '\\' )

        {

            bAddFile = false;

            bAddFolder = true;

        }

        else if( pszFilter[0] == '*' )

        {

            bAddFolder = true;

        }

    }

 

    strcpy_s( buf, MAX_PATH, pszDirectory);

    strcat_s( buf, MAX_PATH, "\\");

    if( bAddFolder == true )

        strcat_s( buf, MAX_PATH, "*.*" );

    else

        strcat_s( buf, MAX_PATH, pszFilter);

 

    HANDLE hHandle = FindFirstFile( buf, &FindFileData );

 

    for( ; hHandle != INVALID_HANDLE_VALUE; )

    {       

        if( ( FindFileData.cFileName[0] != '.' || strlen( FindFileData.cFileName) > 2 ) &&

            ( 0 != _stricmp( FindFileData.cFileName, "Replay"))) //  Replay Æú´õ´Â Áö¿ìÁö ¾Ê´Â´Ù

        {

            strcpy_s( buf, MAX_PATH, pszDirectory );

            strcat_s( buf, MAX_PATH, "\\" );

            strcat_s( buf, MAX_PATH, FindFileData.cFileName );

 

            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

            {

                if( bAddFolder )

                    vtList->push_back(buf);

                GetFindFileList(buf, pszFilter, vtList, iLevel - 1 );

            }

            else

            {

                if( bAddFile )

                    vtList->push_back(buf);

            }

 

        }

 

        if( !FindNextFile(hHandle,&FindFileData) )

            break;

    }

    FindClose(hHandle);

}

 

 

void main()

{

    std::vector <std::string> vtFileList;

    std::vector <std::string>::iterator iter;

 

    GetFindFileList("d:\\work", "*.*", &vtFileList, 1 );

    int FileCount = (int)vtFileList.size();

 

    for ( iter = vtFileList.begin(); iter != vtFileList.end() ; ++iter )

    {

        const char *fileName = iter->c_str();

        std::cout << fileName << std::endl;

    }

}

¼Ò½º :  FindFirstFile.cpp

[Æß]
http://www.codesarang.com/code/content_38.html