如何遍历文件夹及其子文件夹中所有指定的文件

如题所述

只会C++,以下代码复制的,可以实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

使用::FindFirstFile和::FindNextFile方法
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define LEN 1024
int FileCount = 0;
// 深度优先递归遍历目录中所有的文件
BOOL DirectoryList(LPCSTR Path)
{
WIN32_FIND_DATA FindData;
HANDLE hError;

char FilePathName[LEN];
// 构造路径
char FullPathName[LEN];
strcpy(FilePathName, Path);
strcat(FilePathName, "\\*.*");
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf("搜索失败!");
return 0;
}
while(::FindNextFile(hError, &FindData))
{
// 过虑.和..
if (strcmp(FindData.cFileName, ".") == 0
|| strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
// 构造完整路径
wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
FileCount++;
// 输出本级的文件
printf("%s\n", FullPathName);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
DirectoryList(FullPathName);
}
}
return 0;
}
void main()
{
DirectoryList("F:\\123");
printf("共%d个文件\n",FileCount);
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答