2009-04-06 5 views
0

컴퓨터에서 파일을 검색하는 방법은 무엇입니까? 어쩌면 특정 확장자를 찾고있을 수도 있습니다.Windows에서 C로 된 파일 검색

모든 파일을 반복하고 파일 이름을 검사해야합니다.

확장명이 .code 인 파일을 모두 찾고 싶다고 말합니다.

+0

입니까? –

답변

1

FindFirstFile() 또는 FindNextFile() 함수와 하위 폴더를 통과하는 재귀 알고리즘을 사용하십시오.

3

Windows의 경우 FindFirstFile()FindNextFile() 기능을 살펴볼 수 있습니다. 재귀 적 검색을 구현하려는 경우 GetFileAttributes()를 사용하여 FILE_ATTRIBUTE_DIRECTORY을 확인할 수 있습니다. 파일이 실제 디렉토리이면 검색을 계속하십시오.

+0

FindFirst/NextFile은 이미 파일의 속성을 알려줍니다. GetFileAttributes를 호출 할 필요가 없습니다. –

-1

FindFirstFile()/FindNextFile()은 디렉토리에서 파일 목록을 찾는 작업을 수행합니다. 재귀 검색을 수행하려면 하위 디렉토리를 사용하여 _splitpath

경로를 디렉토리와 파일 이름으로 분리 한 다음 결과 디렉토리 세부 정보를 사용하여 재귀 디렉토리 검색을 수행하십시오.

1

FindFirstFile을위한 좋은 래퍼 창에 대한 dirent.h를이 (dirent.h를 토니 Ronkko 구글) 당신이 "를 통해 분석"무엇을 의미합니까


#define S_ISREG(B) ((B)&_S_IFREG) 
#define S_ISDIR(B) ((B)&_S_IFDIR) 

static void 
scan_dir(DirScan *d, const char *adir, BOOL recurse_dir) 
{ 
    DIR *dirfile; 
    int adir_len = strlen(adir); 

    if ((dirfile = opendir(adir)) != NULL) { 
     struct dirent *entry; 
     char path[MAX_PATH + 1]; 
     char *file; 

     while ((entry = readdir(dirfile)) != NULL) 
     { 
      struct stat buf; 
      if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name)) 
       continue; 

      sprintf(path,"%s/%.*s", adir, MAX_PATH-2-adir_len, entry->d_name); 

      if (stat(path,&buf) != 0) 
       continue; 

      file = entry->d_name; 
      if (recurse_dir && S_ISDIR(buf.st_mode)) 
       scan_dir(d, path, recurse_dir); 
      else if (match_extension(path) && _access(path, R_OK) == 0) // e.g. match .code 
       strs_find_add_str(&d->files,&d->n_files,_strdup(path)); 
     } 
     closedir(dirfile); 
    } 
    return; 
}