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
    }
     
  • 相关阅读:
    RWIGS and LORBIT (1)
    时间档案:飞秒、皮秒、纳秒、微秒、毫秒、秒 (转自新浪)
    Linux Shell 文本处理工具集锦(转载)
    awk——getline
    PERL 正则表达式简介
    算法的性能
    排序算法与稳定性的理解
    实现双端队列
    实现栈
    实现队列
  • 原文地址:https://www.cnblogs.com/ahuo/p/1025197.html
Copyright © 2011-2022 走看看