如何列举一个目录下的文件
调用该函数之后,output窗口输出文件名以及文件大小信息:
飘飘白云 2008.04.04
取得一个目录路径之后,使用FindFirstFile与FindNextFile就可以列举该目录的所有子目录以及所有文件。下面贴段列举指定目录下所有特定后缀(.txt)文件。
1
voidgetTxtFileByDirectory(constCStringW&dirPath)
2

{
3
nutVec.clear();
4
path=dirPath;
5
6
WIN32_FIND_DATAffd;
7
LARGE_INTEGERfilesize;
8
HANDLEhFind;
9
10
CStringWnutFile;
11
nutFile.Format(L"%s//*.txt",path);
12
13
//Checkthattheinputpathplus2isnotlongerthanMAX_PATH.
14
size_tlength;
15
StringCchLength(nutFile,MAX_PATH,&length);
16
17
if(length>(MAX_PATH-2))
{
18
CStringWdbgInfo;
19
dbgInfo.Format(L"Directory%sistoolong!/n",path);
20
OutputDebugString(dbgInfo);
21
return;
22
}
23
24
hFind=FindFirstFile(nutFile,&ffd);
25
if(hFind==INVALID_HANDLE_VALUE)
{
26
return;
27
}
28
29
do
{
30
filesize.LowPart=ffd.nFileSizeLow;
31
filesize.HighPart=ffd.nFileSizeHigh;
32
33
CStringWdbgInfo;
34
dbgInfo.Format(L"%s%ldbytes/n",ffd.cFileName,filesize.QuadPart);
35
OutputDebugString(dbgInfo);
36
37
nutVec.push_back(ffd.cFileName);
38
}while(FindNextFile(hFind,&ffd)!=0);
39
40
FindClose(hFind);
41
}
voidgetTxtFileByDirectory(constCStringW&dirPath)2


{3
nutVec.clear();4
path=dirPath;5

6
WIN32_FIND_DATAffd;7
LARGE_INTEGERfilesize;8
HANDLEhFind;9

10
CStringWnutFile;11
nutFile.Format(L"%s//*.txt",path);12

13
//Checkthattheinputpathplus2isnotlongerthanMAX_PATH.14
size_tlength;15
StringCchLength(nutFile,MAX_PATH,&length);16

17

if(length>(MAX_PATH-2))
{18
CStringWdbgInfo;19
dbgInfo.Format(L"Directory%sistoolong!/n",path);20
OutputDebugString(dbgInfo);21
return;22
}23

24
hFind=FindFirstFile(nutFile,&ffd);25

if(hFind==INVALID_HANDLE_VALUE)
{26
return;27
}28

29

do
{30
filesize.LowPart=ffd.nFileSizeLow;31
filesize.HighPart=ffd.nFileSizeHigh;32

33
CStringWdbgInfo;34
dbgInfo.Format(L"%s%ldbytes/n",ffd.cFileName,filesize.QuadPart);35
OutputDebugString(dbgInfo);36

37
nutVec.push_back(ffd.cFileName);38
}while(FindNextFile(hFind,&ffd)!=0);39

40
FindClose(hFind);41
}
调用该函数之后,output窗口输出文件名以及文件大小信息:
0000000000000001.txt365bytes
0000000000000002.txt494bytes
0000000000000003.txt352bytes
0000000000000004.txt468bytes
0000000000000005.txt366bytes
0000000000000006.txt461bytes
0000000000000007.txt375bytes
000000000000000a.txt375bytes
000000000000000b.txt382bytes
0000000000000002.txt494bytes
0000000000000003.txt352bytes
0000000000000004.txt468bytes
0000000000000005.txt366bytes
0000000000000006.txt461bytes
0000000000000007.txt375bytes
000000000000000a.txt375bytes
000000000000000b.txt382bytes
关于路径的取得,可以使用打开文件对话框获得,相应代码如下:
1
OPENFILENAMEWofn;//commondialogboxstructure
2
wchar_tszFile[260];//bufferforfilename
3
ZeroMemory(&ofn,sizeof(ofn));
4
ofn.lStructSize=sizeof(ofn);
5
ofn.hwndOwner=NULL;
6
ofn.lpstrFile=szFile;
7
ofn.lpstrTitle=L"Selectanutfiletosetadirectoryforautoload
";
8
ofn.lpstrFile[0]=L'/0';
9
ofn.nMaxFile=sizeof(szFile);
10
ofn.lpstrFilter=L"nut/0*.nut";
11
ofn.nFilterIndex=1;
12
ofn.lpstrFileTitle=NULL;
13
ofn.nMaxFileTitle=0;
14
ofn.lpstrInitialDir=path;
15
ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
16
17
//Openfiledialogbox.
18
if(GetOpenFileNameW(&ofn)==TRUE)
{
19
if(ofn.lpstrFile)
{
20
size_tlen=wcslen(ofn.lpstrFile);
21
szFile[len]=L'/0';
22
23
CStringWfullPath;
24
fullPath.Format(L"%s",szFile);
25
26
HLUint32n=fullPath.ReverseFind(L'//');
27
if(n>0)
{
28
path=fullPath.Left(n);
29
}
30
}
31
}
OPENFILENAMEWofn;//commondialogboxstructure2
wchar_tszFile[260];//bufferforfilename3
ZeroMemory(&ofn,sizeof(ofn));4
ofn.lStructSize=sizeof(ofn);5
ofn.hwndOwner=NULL;6
ofn.lpstrFile=szFile;7
ofn.lpstrTitle=L"Selectanutfiletosetadirectoryforautoload
";8
ofn.lpstrFile[0]=L'/0';9
ofn.nMaxFile=sizeof(szFile);10
ofn.lpstrFilter=L"nut/0*.nut";11
ofn.nFilterIndex=1;12
ofn.lpstrFileTitle=NULL;13
ofn.nMaxFileTitle=0;14
ofn.lpstrInitialDir=path;15
ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;16

17
//Openfiledialogbox.18

if(GetOpenFileNameW(&ofn)==TRUE)
{19

if(ofn.lpstrFile)
{20
size_tlen=wcslen(ofn.lpstrFile);21
szFile[len]=L'/0';22

23
CStringWfullPath;24
fullPath.Format(L"%s",szFile);25

26
HLUint32n=fullPath.ReverseFind(L'//');27

if(n>0)
{28
path=fullPath.Left(n);29
}30
}31
}
本文介绍了一种使用FindFirstFile与FindNextFile函数来枚举指定目录下所有.txt文件的方法,并提供了完整的C++代码实现。此外,还展示了如何通过文件对话框获取目标路径。
2118

被折叠的 条评论
为什么被折叠?



