zoukankan      html  css  js  c++  java
  • vc遍历目录

    #include "stdafx.h" 
    #include 
    <windows.h> 
    #define FILEILTER 
    "*.*"

    BOOL IsRoot(LPCTSTR lpszPath) 

        TCHAR szRoot[
    4]; 
        wsprintf(szRoot, 
    "%c:\\", lpszPath[0]); 
        
    return (lstrcmp(szRoot, lpszPath) == 0); 
    }
     

    void FindInAll(LPCTSTR lpszPath) 
    {
        TCHAR szFind[MAX_PATH]; 
        lstrcpy(szFind, lpszPath); 
        
    if (!IsRoot(szFind)) 
            lstrcat(szFind, 
    "\\"); 
        lstrcat(szFind, FILEILTER); 
    // 找所有文件 
        WIN32_FIND_DATA wfd; 
        HANDLE hFind 
    = FindFirstFile(szFind, &wfd); 
        
    if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败 
            return
        
        
    do 
        

            
    if (wfd.cFileName[0== '.'
                
    continue// 过滤这两个目录 
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
            

                TCHAR szFile[MAX_PATH]; 
                
    if (IsRoot(lpszPath)) 
                    wsprintf(szFile, 
    "%s%s", lpszPath, wfd.cFileName); 
                
    else 
                
    {
                    wsprintf(szFile, 
    "%s\\%s", lpszPath, wfd.cFileName); 
                    FindInAll(szFile); 
    // 如果找到的是目录,则进入此目录进行递归 
                }

            }
     
            
    else 
            

                TCHAR szFile[MAX_PATH]; 
                
    if (IsRoot(lpszPath)) 
                
    {
                    wsprintf(szFile, 
    "%s%s", lpszPath, wfd.cFileName); 
                }

                
    else 
                
    {
                    wsprintf(szFile, 
    "%s\\%s", lpszPath, wfd.cFileName); 
                    printf(
    "%s\n",szFile); 
                }

                
    // 对文件进行操作 
            }
     
        }
     while (FindNextFile(hFind, &wfd)); 
        FindClose(hFind); 
    // 关闭查找句柄 
        
    }
     
    int main(int argc, char* argv[]) 

        FindInAll(
    "C:\\TEST"); 
        
    return 0
    }
     
  • 相关阅读:
    48. Rotate Image
    83. Remove Duplicates from Sorted List
    46. Permutations
    HTML5笔记
    18. 4Sum
    24. Swap Nodes in Pairs
    42. Trapping Rain Water
    Python modf() 函数
    Python min() 函数
    Python max() 函数
  • 原文地址:https://www.cnblogs.com/ahuo/p/1025197.html
Copyright © 2011-2022 走看看