C 給定路徑遍歷目錄下的所有文件


在此之前需要了解 WIN32_FIND_DATA的結構 以及  FindFirstFile、 FindNextFile原型以及用法注意事項傳送門如下

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx

 

涉及的宏定義

------------------------------------------------------------------------------------------------------------------------------------------------

  • FILE_ATTRIBUTE_ARCHIVE
    32 (0x20)
A file or directory that is an archive file or directory. Applications typically use this attribute to markfiles for backup or removal.存檔類
  • FILE_ATTRIBUTE_COMPRESSED
    2048 (0x800)
A file or directory that is compressed. For a file, all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_DEVICE
    64 (0x40)
This value is reserved for system use.驅動類
  • FILE_ATTRIBUTE_DIRECTORY
    16 (0x10)
The handle that identifies a directory.目錄類
  • FILE_ATTRIBUTE_ENCRYPTED
    16384 (0x4000)
A file or directory that is encrypted. For a file, all data streams in the file are encrypted. For a directory, encryption is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_HIDDEN
    2 (0x2)
The file or directory is hidden. It is not included in an ordinary directory listing.隱藏
  • FILE_ATTRIBUTE_INTEGRITY_STREAM
    32768 (0x8000)
The directory or user data stream is configured with integrity (only supported on ReFS volumes). It is not included in an ordinary directory listing. The integrity setting persists with the file if it's renamed. If a file is copied the destination file will have integrity set if either the source file or destination directory have integrity set.
Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows Server2012.
  • FILE_ATTRIBUTE_NORMAL
    128 (0x80)
A file that does not have other attributes set. This attribute is valid only when used alone.普通
  • FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
    8192 (0x2000)
The file or directory is not to be indexed by the content indexing service.
  • FILE_ATTRIBUTE_NO_SCRUB_DATA
    131072 (0x20000)
The user data stream not to be read by the background data integrity scanner (AKA scrubber). When set on a directory it only provides inheritance. This flag is only supported on Storage Spaces and ReFS volumes. It is not included in an ordinary directory listing.
Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows8 and Windows Server2012.
  • FILE_ATTRIBUTE_OFFLINE
    4096 (0x1000)
The data of a file is not available immediately. This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is the hierarchical storage management software. Applications should not arbitrarily change this attribute.
  • FILE_ATTRIBUTE_READONLY
    1 (0x1)
A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories. For more information, see You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7.
  • FILE_ATTRIBUTE_REPARSE_POINT
    1024 (0x400)
A file or directory that has an associated reparse point, or a file that is a symbolic link.
  • FILE_ATTRIBUTE_SPARSE_FILE
    512 (0x200)
A file that is a sparse file.
  • FILE_ATTRIBUTE_SYSTEM
    4 (0x4)
A file or directory that the operating system uses a part of, or uses exclusively.系統文件
  • FILE_ATTRIBUTE_TEMPORARY
    256 (0x100)
A file that is being used for temporary storage. File systems avoid writing data back to mass storage ifsufficient cache memory is available, because typically, an application deletes a temporary file after the handleis closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written afterthe handle is closed.臨時文件
  • FILE_ATTRIBUTE_VIRTUAL
    65536 (0x10000)
This value is reserved for system use.虛擬文件(系

出自:https://baike.baidu.com/item/WIN32_FIND_DATA

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我的理解上述宏定義

●FILE_ATTRIBUTE_ARCHIVE——文件包含歸檔屬性。
●FILE_ATTRIBUTE_COMPRESSED——文件和目錄被壓縮。
●FILE_ATTRIBUTE_DIRECTORY——找到的是一個目錄。
●FILE_ATTRIBUTE_HIDDEN——文件包含隱含屬性。
●FILE_ATTRIBUTE_NORMAL——文件沒有其他屬性。
●FILE_ATTRIBUTE_READONLY——文件包含只讀屬性。
●FILE_ATTRIBUTE_SYSTEM——文件包含系統屬性。
●FILE_ATTRIBUTE_TEMPORARY——文件是一個臨時文件
 
typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes; //文件屬性
FILETIME ftCreationTime; // 文件創建時間
FILETIME ftLastAccessTime; // 文件最后一次訪問時間
FILETIME ftLastWriteTime; // 文件最后一次修改時間
DWORD nFileSizeHigh; // 文件長度高32位
DWORD nFileSizeLow; // 文件長度低32位
DWORD dwReserved0; // 系統保留
DWORD dwReserved1; // 系統保留
TCHAR cFileName[ MAX_PATH ]; // 文件名
TCHAR cAlternateFileName[ 14 ]; // 格式文件名
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;
 
HANDLE FindFirstFile(
    LPCTSTR lpFileName,//filename
    LPWIN32_FIND_DATA lpFindFileData//databuffer
);

參數說明

LPCTSTR lpFileName文件名(包括路徑)
LPWIN32_FIND_DATA lpFindFileData 指向一個用於保存文件信息的結構體

返回值

如果調用成功返回一個句柄,可用來做為FindNextFile或 FindClose參數
調用失敗 返回為INVALID_HANDLE_VALUE(即-1) ,可調用GetLastError來獲取錯誤信息
 
BOOL FindNextFile(
HANDLE hFindFile, //searchhandle
LPWIN32_FIND_DATA lpFindFileData //databuffer
);

參數說明

HANDLE hFindFile搜索的 文件句柄 函數執行的時候搜索的是此句柄的下一文件
LPWIN32_FIND_DATA lpFindFileData 指向一個用於保存文件信息的 結構體
 

返回值

非零表示成功,零表示失敗。如不再有與指定條件相符的文件,會將 GetLastError設置成ERROR_NO_MORE_FILES
 
上述參數解析來源 百度百科,然后准備工作已備好,那么以下是實例,比較簡單,沒有改成通用的,修改並不難若有疑問/錯誤望大神提出:
header files:

#include <stdio.h>
#include <string>

#define ParaPath "path"

 

source files:

#include "loadfileDemo.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <IO.h>

//緩存大小

#define LEN 1024   


//給定路徑查詢該目錄下所有文件,並輸出文件名跟路徑
bool find(char * lpPath)
{
  char findPath[LEN];  
  WIN32_FIND_DATA FindFileData; //首先了解 WIN32_FIND_DATA結構
  strcpy(findPath,lpPath);
  strcat(findPath,"*.*");  

  HANDLE hFind=::FindFirstFile(findPath,&FindFileData);// 路徑,查找緩沖區為形參
  if(INVALID_HANDLE_VALUE == hFind) //查找失敗 宏定義為無效值 0xFFFFFFFF 即-1
  return false;


  while(TRUE)
  {
    if(FindFileData.dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY) //判斷是否是文件
  {
      if(FindFileData.cFileName[0]!='.')//排除根目錄\.. \.
    {
      strcpy(findPath,lpPath);
      strcat(findPath,FindFileData.cFileName);
      printf("findPath:%s\n",findPath);
      printf("FileData:%s\n",FindFileData.cFileName);
    }
      if(!FindNextFile(hFind,&FindFileData)) //目錄不為空指針往后移動
        break;
    }
  }
    FindClose(hFind);
    return true;
}

int main(void)
{
    char *str = ParaPath;


     if(find(str)) //傳入需要查找的路徑
      {
        printf("success!\n");
        }
     else
      {
        printf("fail!\n");
      }
      return 0;
}

注意:限定windows下使用,接口為windows提供的,若要跨平台可考慮Qt/C++


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM