一、查找指定的文件
1、了解系統(tǒng)結(jié)構(gòu)體和系統(tǒng)函數(shù)
結(jié)構(gòu)體為 struct _finddata_t,
函數(shù)為:_findfirst、findnext和 _findclose。
結(jié)構(gòu)體struct finddata
struct _finddata_t
{
unsigned attrib;//文件屬性 _A_ARCH(存檔)、_A_HIDDEN(隱藏)、_A_NORMAL(正常)、_A_RDONLY(只 讀)、_A_SUBDIR(文件夾)、_A_SYSTEM(系統(tǒng))
time_t time_create;//文件創(chuàng)建時間
time_t time_access;//文件最后一次被訪問的時間。
time_t time_write;//文件最后一次被修改的時間
_fsize_t size;//文件的大小
char name[_MAX_FNAME];
};
函數(shù):_findfirst、findnext 和 _findclose
2、long _findfirst( char *filespec, struct _finddata_t fileinfo )
返回值:如果查找成功的話,將返回一個long型的唯一的查找用的句柄(就是一個唯一編號)。這個句柄將在_findnext函數(shù)中被使用。若失敗,則返回-1。
參數(shù):
filespec:標(biāo)明文件的字符串,可支持通配符。比如:.c,則表示當(dāng)前文件夾下的所有后綴為C的文件。
fileinfo :這里就是用來存放文件信息的結(jié)構(gòu)體的指針。這個結(jié)構(gòu)體必須在調(diào)用此函數(shù)前聲明,不過不用初始化,只要分配了內(nèi)存空間就可以了。函數(shù)成功后,函數(shù)會把找到的文件的信息放入這個結(jié)構(gòu)體中。
3、int _findnext( long handle, struct _finddata_t *fileinfo )
返回值:若成功返回0,否則返回-1。
參數(shù):
handle:即由_findfirst函數(shù)返回回來的句柄。
fileinfo:文件信息結(jié)構(gòu)體的指針。找到文件后,函數(shù)將該文件信息放入此結(jié)構(gòu)體中。
4、int _findclose( long handle )
返回值:成功返回0,失敗返回-1。
參數(shù):
handle :_findfirst函數(shù)返回回來的句柄。
5、例子
#include<stdio.h>
#include<io.h>
const char to_search=”C:\WINDOWS\.txt”; //欲查找的文件,支持通配符
int main()
{
long handle; //用于查找的句柄
struct _finddata_t fileinfo; //文件信息的結(jié)構(gòu)體
handle =_findfirst(to_search,&fileinfo); //第一次查找
if(-1==handle)
return -1;
printf(“%s\n”,fileinfo.name);
while(!_findnext(handle,&fileinfo)) //循環(huán)查找其他符合的文件,知道找不到其他的為止
{
printf(“%s\n”,fileinfo.name);
}
_findclose(handle);
system("pause");
return 0;
}
二、查找指定的文件夾
1、了解系統(tǒng)結(jié)構(gòu)體和系統(tǒng)函數(shù)
結(jié)構(gòu)體為 struct WIN32_FIND_DATAA,函數(shù)為:FindFirstFile、FindNextFile和 FindClose。
結(jié)構(gòu)體 WIN32_FIND_DATAA_

其中文件屬性為
現(xiàn)在針對 GetFileAttributes 函數(shù)的返回值做以下整理
返回字段 返回值 屬性類型
宏值 數(shù)值 代表意思
FILE_ATTRIBUTE_READONLY 1 只讀
FILE_ATTRIBUTE_HIDDEN 2 隱藏
FILE_ATTRIBUTE_SYSTEM 4 系統(tǒng)
FILE_ATTRIBUTE_DIRECTORY 16 目錄
FILE_ATTRIBUTE_ARCHIVE 32 存檔
FILE_ATTRIBUTE_DEVICE 64 保留
FILE_ATTRIBUTE_NORMAL 128 正常
FILE_ATTRIBUTE_TEMPORARY 256 臨時
FILE_ATTRIBUTE_SPARSE_FILE 512 稀疏文件
FILE_ATTRIBUTE_REPARSE_POINT 1024 超鏈接或快捷方式
FILE_ATTRIBUTE_COMPRESSED 2048 壓縮
FILE_ATTRIBUTE_OFFLINE 4096 脫機(jī)
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 8192 索引
FILE_ATTRIBUTE_ENCRYPTED 16384 加密
FILE_ATTRIBUTE_VIRTUAL 65536 虛擬
2、例子
#include <iostream>
#include <string>
#include <windows.h>
int find_path(const char *);
char save_path[MAX_PATH] = { 0 };//結(jié)果輸出路徑;
char szPath[MAX_PATH] = { 0 };
char result[MAX_PATH] = { 0 };
FILE* pf_path_file;int find_path(const char *);
int find_path_save_file(const char* lp_path, const char* out_file_name)
{
pf_path_file = fopen(out_file_name, "w");
int cnt = find_path(lp_path);
fclose(pf_path_file);
return cnt;
}
void str_tok(char *str) //分離文件目錄
{
char *p = NULL;
char delims[] = "\";
p = strtok(str, delims);
while (p != NULL)
{
strcpy(result, p);
p = strtok(NULL, delims);
};
}
void str_extension(char *str) //分離文件擴(kuò)展名;
{
char *p = NULL;
char delims[] = ".";
p = strtok(str, delims);
while (p != NULL)
{
strcpy(result, p);
p = strtok(NULL, delims);
};
}
int find_path(const char* lp_path)// 遍歷搜索目錄
{
static int cnt = 0;
HANDLE hFile;
WIN32_FIND_DATAA wfd; //數(shù)據(jù)結(jié)構(gòu);
char sz_path[MAX_PATH] = { 0 };
char buf[MAX_PATH * 2] = { 0 };
char fileName[MAX_PATH] = { 0 };
char temp[MAX_PATH] = { 0 };//臨時數(shù)組;
char parseFileName[MAX_PATH] = { 0 };
strcpy(sz_path, lp_path);
strcat(sz_path, "\\*.*"); //匹配任何文件包括文件夾目錄;
hFile = FindFirstFile(sz_path, &wfd);
if (hFile != INVALID_HANDLE_VALUE)
{
do
{
if (wfd.cFileName[0] == '.') //如果是.或..則過濾;
continue;
else if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //如果是目錄則遞歸;
{
strcpy(sz_path, lp_path);
strcat(sz_path, "\\");
if (!strcmp(wfd.cFileName, "test1"))
{
fprintf(pf_path_file, "%s:%s\n", lp_path, wfd.cFileName);
}
if (!strcmp(wfd.cFileName, "test2"))
{
fprintf(pf_path_file, "%s:%s\n", lp_path, wfd.cFileName);
}
strcat(sz_path, wfd.cFileName); //拼接目錄全路徑名;
find_path(sz_path); //調(diào)用遞歸;
}
} while (FindNextFile(hFile, &wfd));
}
return cnt;
}
int _tmain(int argc, _TCHAR* argv[])
{
GetCurrentDirectory(MAX_PATH, szPath); //當(dāng)前目錄
strcpy(save_path, szPath);//結(jié)果result.txt放在當(dāng)前目錄中;
strcat(save_path, "\\result.txt");
find_path_save_file("C:\\Users\\Administrator", save_path);
system("pause");
return 0;
}