zoukankan      html  css  js  c++  java
  • CFileFind类的详解以及应用实例

        CFileFind类在afx.h头文件中声明。
        功能:执行本地文件的查找,支持通配符。
        类的成员函数:

        1、查找操作类:

    1  //搜索目录下指定的文件,成功返回非0。第二个参数不必理会
    2 virtual BOOL FindFile(LPCTSTR pstrName = NULL,DWORD dwUnused = 0); 3 virtual BOOL FindNextFile( );
    4 //继续先前的搜索,在调用此函数之前需先调用FindFile函数,成功返回非0.
        2、获取文件属性类:
     1 virtual BOOL GetCreationTime(FILETIME* pTimeStamp) const;
     2 virtual BOOL GetCreationTime(CTime& refTime) const;  
     3 //获取文件创建时间,成功返回非0。
     4 virtual CString GetFileName( ) const; 
     5 //获取文件名称,包括扩展名。例“西游记.txt”
     6 virtual CString GetFilePath( ) const;
     7 //获取文件的绝对路径。例“D:\\我的文档\\小说\\古典\\西游记.txt”
     8 virtual CString GetFileTitle( ) const;
     9  //获取文件的名称,不包括扩展名。例“西游记”
    10 virtual BOOL GetLastWriteTime(FILETIME* pTimeStamp) const;
    11 virtual BOOL GetLastWriteTime(CTime& refTime) const;  
    12 //获取文件最后一次修改时间。成功返回非0.
    13 virtual BOOL GetLastAccessTime(CTime& refTime) const;
    14 virtual BOOL GetLastAccessTime(FILETIME* pTimeStamp) const;
    15  //获取文件最后一次被访问的时间,成功返回非0.
    16 ULONGLONG GetLength( ) const; 
    17 //获取文件的长度,以字节为单位
    18 virtual CString GetRoot( ) const;  
    19 //获取文件的根目录,例“D:\\我的文档\\小说\\古典\\”
        3、判断文件属性类:
     1 BOOL IsArchived( ) const; //判断文件是否是档案文件
     2     BOOL IsCompressed( ) const; //判断文件是否是压缩文件
     3     BOOL IsDirectory( ) const;  //判断文件是否是目录
     4     virtual BOOL IsDots( ) const; //判断文件是否是“.”和“..” 
     5     BOOL IsHidden( ) const; // 判断文件是否是隐藏文件
     6     BOOL IsNormal( ) const; //判断文件是否是常规文件
     7     BOOL IsReadOnly( ) const; //判断文件是否是只读文件
     8     BOOL IsSystem( ) const;  //判断文件是否是系统文件
     9     BOOL IsTemporary( ) const; //判断文件是否是临时文件
    10     virtual BOOL MatchesMask(DWORD dwMask) const; 
    11  //判断文件的综合属性。
        dwMask参数的使用方法,几种文件属性采用或运算:
    1     FILE_ATTRIBUTE_ARCHIVE:        档案文件  
    2     FILE_ATTRIBUTE_COMPRESSED:     压缩文件   
    3     FILE_ATTRIBUTE_DIRECTORY:      路径文件
    4     FILE_ATTRIBUTE_NORMAL:         正常文件   
    5     FILE_ATTRIBUTE_READONLY:       只读文件
    6     FILE_ATTRIBUTE_SYSTEM:         系统文件  
    7     FILE_ATTRIBUTE_TEMPORARY:      临时文件
    8     FILE_ATTRIBUTE_HIDDEN:         隐藏文件

    遍历指定目录

     1     void BrowseDir(CString strDir)
     2     {
     3         CFileFind ff;
     4         CString szDir = strDir;
     5         if(szDir.Right(1) != "\\")
     6             szDir += "\\";
     7         szDir += "*.*";
     8         BOOL res = ff.FindFile(szDir);
     9         while(res)
    10         {
    11             res = ff.FindNextFile();
    12             if(ff.IsDirectory() && !ff.IsDots())
    13             {
    14                 //如果是一个子目录,用递归继续往深一层找
    15                 BrowseDir(ff.GetFilePath());
    16             }else if(!ff.IsDirectory() && !ff.IsDots())
    17             {
    18                 //显示当前访问的文件
    19                 CStatic* p = 
    20 (CStatic*)GetDlgItem(IDC_STATIC_FILE);
    21                 CString str;
    22                 str.Format("当前访问的文
    23                            件:%s",ff.GetFilePath());
    24                 p->SetWindowText(str);
    25                 Sleep(500);
    26             }
    27         }
    28         ff.Close();//关闭
    29     }
    查找指定的文件:
     1     void SearchFile(CString strDir,CString strFile)
     2     {
     3         CFileFind ff;
     4         CString szDir = strDir;
     5         if(szDir.Right(1) != "\\")
     6         szDir += "\\";
     7         szDir += "*.*";
     8         BOOL res = ff.FindFile(szDir);
     9         while(res)
    10         {
    11             res = ff.FindNextFile();
    12             if(ff.GetFileName()==strFile)
    13             {
    14                 //找到了,加入列表框中
    15                 m_ctrlFilesList.AddString(ff.GetFilePath());
    16             }
    17             if(ff.IsDirectory() && !ff.IsDots())
    18             {
    19                 //如果是一个子目录,用递归继续往深一层找
    20                 SearchFile(ff.GetFilePath(),strFile);
    21             }
    22         }
    23         ff.Close();//关闭
    24     }

    删除整个目录:

     1     void RecursiveDelete(CString szPath)
     2     {
     3         CFileFind ff;
     4         CString path = szPath;
     5         if(path.Right(1) != "\\")
     6         path += "\\";
     7         path += "*.*";
     8         BOOL res = ff.FindFile(path);
     9         while(res)
    10         {
    11             res = ff.FindNextFile();//是文件时直接删除
    12             if (!ff.IsDots() && !ff.IsDirectory())
    13                 DeleteFile(ff.GetFilePath());
    14             else if (ff.IsDots())
    15                 continue;
    16             else if (ff.IsDirectory())
    17             {
    18                 path = ff.GetFilePath();
    19                 //是目录时继续递归,删除该目录下的文件
    20                 RecursiveDelete(path);
    21                 //目录为空后删除目录
    22                 RemoveDirectory(path);  
    23                  //删除空目录,若目录不为空则返回0
    24             }
    25         }
    26         //最终目录被清空了,于是删除该目录
    27         RemoveDirectory(szPath);
    28      
    29     }
  • 相关阅读:
    选择排序
    冒泡排序
    排序介绍
    如何在服务器搭建JavaWeb项目环境(阿里轻量级)
    SSM整合配置文件
    如何删干净MySQL数据库
    spring概述
    Git简单命令
    第六天——读操作(二)
    第六天——文件操作(一)
  • 原文地址:https://www.cnblogs.com/freeabyss/p/3187070.html
Copyright © 2011-2022 走看看