C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[48231] 특정경로(들)에 위치한 파일들의 갯수를 알고 싶습니다.
손성민 [] 1125 읽음    2007-02-27 22:33
볼랜드툴을 사용하진 않지만..^^;

특정경로가 포함되는 모든 하위경로들을 포함해서... 파일의 갯수를 카운트하는 놈입니다.
findfirstfile()  / findnextfile() 함수를 이용해서......
현재 아래와 같이 구현을 했씁니다..

그런데 문제는......엄청난 I/O 발생과 함께.... 가장 중요한 시간이 너무나 오래 걸린다는 겁니다..ㅠ_ㅡ
이 문제를 근복적..혹은 부분적으로라도 해결할 수 있는 방법이 없을까요?
특별히 알고리즘의 수정은 필요없을 거라고 생각합니다만..(물론;;; 파일시스템 구조를 전혀 모르기 떔에...ㅠㅠ)
작은 도움이라도 주시면 감사드리겠습니다.



#include <windows.h>
#include "sys/stat.h"
#include <stdio.h>

long ResearchFileNumber(char *szSearchPath);
long FindFileRecursive(char *_path);

/*************************************************************************
//    Test main()

void main()
{
    char szSearchPath[]="c:\\windows";    //Search path
    printf("%d", ResearchFileNumber(szSearchPath));
}

*************************************************************************/

long ResearchFileNumber(char *szSearchPath)
{
    long nCounter=0;

    struct _stat stat;
    int            iResult;
    char        cBuf[MAX_PATH * 2 + 1 ]="";

    iResult = _stat(szSearchPath, &stat);

    if(iResult == 0)
    {
        if(stat.st_mode & _S_IFDIR)    //Directory
            wsprintf(cBuf,"%s*.*",szSearchPath);
        else
            wsprintf(cBuf,"%s",szSearchPath);
        return FindFileRecursive(cBuf);
    }
    return -1;
}

long FindFileRecursive(char *_path)
{
    long        nFileCounter=0;
    long        nTempCounter=0;
   
    HANDLE    hSrch;
    WIN32_FIND_DATA  wfd;
    BOOL        bResult = TRUE;

    char        fname[MAX_PATH]="";
    char        drive[_MAX_DRIVE]="";
    char        dir[MAX_PATH]="";
    char        newpath[MAX_PATH]="";

    hSrch = FindFirstFile(_path, &wfd);

    if(hSrch == INVALID_HANDLE_VALUE)
        return -1;

    while(bResult)
    {
        _splitpath(_path, drive, dir, NULL, NULL);
        if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)   
        {
            if(wfd.cFileName[0] != '.')   
            {
                wsprintf(newpath,"%s%s%s\\*.*", drive, dir, wfd.cFileName);
                nTempCounter = FindFileRecursive(newpath);   
                nFileCounter += nTempCounter;
            }
        }
        else   
            nFileCounter++;   
        bResult = FindNextFile(hSrch, &wfd);

        if(hSrch==INVALID_HANDLE_VALUE)   
        {
            while(hSrch!=INVALID_HANDLE_VALUE)
                bResult = FindNextFile(hSrch, &wfd);
        }
    }
    FindClose(hSrch);
    return nFileCounter;
}

+ -

관련 글 리스트
48231 특정경로(들)에 위치한 파일들의 갯수를 알고 싶습니다. 손성민 1125 2007/02/27
48233     Re:특정경로(들)에 위치한 파일들의 갯수를 알고 싶습니다. kylix 1360 2007/02/28
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.