非遞迴方式依檔案類型列舉

非遞迴方式依檔案類型列舉

  1. Non-recursive list file win32 version
    通常看到的版本都是遞迴列舉多層次目錄下的檔案,這裡舉個非遞迴列舉檔案的方法。利用stack來暫存資料夾路徑,每次從stack pop目錄路徑,列舉其下的名稱(檔案或目錄),若是檔案則印出,若是目錄且非 "." 則 push in stack。當stack為空時則終止。
    
    int scan_file_dir(char *szDir)
    {
        HANDLE hFile;
        WIN32_FIND_DATA wfd;
        char szPath[MAX_PATH];
        char szFile[MAX_PATH];
        stack directory;
        int  nFile = 0;
        
        int nlen = strlen(szDir);
        strcpy(szPath, szDir);
        
        if (szPath[nlen-1] == '\\' || szPath[nlen-1] == '/')
        szPath[nlen-1] = '\0';
        
        //strcpy(szDir[nDir], szPath);
        directory.push(szPath);
        
        while(directory.size()> 0) {
            
            strcpy(szPath, directory.top().c_str());
            sprintf(szFile, "%s/*.*", szPath);
            directory.pop();
            hFile = FindFirstFile(szFile, &wfd);
            
            if(hFile != INVALID_HANDLE_VALUE)
            {
                do {
                    if(!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
                    {
                        char szTmp[MAX_LINE];
                        sprintf(szTmp, "%s/%s", szPath, wfd.cFileName);
                        printf("%s\n", szTmp);
                        nFile++;
                    }
                    else if((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)&&(wfd.cFileName[0] != '.'))
                    {
                        char   szPathName[MAX_LINE];
                        sprintf(szPathName, "%s/%s", szPath, wfd.cFileName);
                        directory.push(szPathName);
                    }
                } while(FindNextFile(hFile, &wfd));
                FindClose(hFile);
            }
        }
        return nFile;
    }
  2. Non-recursive list file unix version
    列舉出副檔名為mp3、ogg、wma的檔案
    
    int scan_song_dir(char *szDir)
    {
        DIR           *dp;
        struct dirent *ep;
        struct stat st;
        char   szExt[5];
        char   szPath[MAX_PATH];
        char   szFile[MAX_PATH];
        int      nlen;
        stack directory;
        int    nSong = 0;
        
        nlen = strlen(szDir);
        strcpy(szPath, szDir);
        
        if (szPath[nlen -1] != '/' && szPath[nlen -1] != '\\')
        strcat(szPath, "/");
        
        directory.push(szPath);
        
        while(directory.size()> 0) {
            
            strcpy(szPath, directory.top().c_str());
            directory.pop();
            
            dp  =  opendir  (szPath);
            if  (dp  !=  NULL) {
                while  (ep  =  readdir  (dp)) {
                    
                    char   szPathName[MAX_LINE] = {'\0'};
                    
                    if(ep->d_name[0] == '.')
                    continue;
                    
                    strcat(szPathName, szPath);
                    strcat(szPathName, ep->d_name);
                    
                    //printf("path: %s\n", szPathName);
                    //sprintf(szPathName, "%s/%s", szPath, ep->d_name);
                    stat(szPathName, &st);
                    
                    if(S_ISDIR(st.st_mode)) {
                        strcat(szPathName, "/");
                        printf("append dir : %s\n", szPathName);
                        directory.push(szPathName);
                        
                    }
                    else {
                        nlen = strlen(ep->d_name);
                        if (nlen <5)
                        continue;
                        strncpy(szExt, ep->d_name + nlen - 4, 4);
                        szExt[4] = '\0';
                        
                        if(strstr(szExt, ".mp3") || strstr(szExt, ".wma") || strstr(szExt, ".ogg")) {
                            
                            printf("Song Path: %s\n", szPathName);
                            
                            nSong++;
                        }
                    }
                }
                closedir(dp);
            }
            else
            printf("Cannot open the %s directory.\n", szPath);
        }
        
        return nSong;
    }
Dotblogs 的標籤: ,