自己给出一个文件夹,把该文件夹中所有的文件名写入一个自定义txt文件中保存
现在不能遍历子文件夹
WIN32_FIND_DATAA fileData;
char strfile[1024] = {};
HANDLE hflie = CreateFile(_T("遍历结果.txt"),
GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_NEW,FILE_ATTRIBUTE_NORMAL, nullptr);
HANDLE hfiandfile = FindFirstFileA("../遍历文件夹/*.*",&fileData);
memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(strfile));
strcat_s(strfile, "\r\n");
DWORD len = 0;
WriteFile(hflie, strfile, strlen(strfile), &len, nullptr);
while (FindNextFileA(hfiandfile, &fileData))
{
ZeroMemory(strfile, sizeof(char)* 1024);
memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(fileData.cFileName));
strcat_s(strfile, "\r\n");
WriteFile(hflie, strfile, strlen(strfile), &len, nullptr);
}
CloseHandle(hflie);
CloseHandle(hfiandfile);
return (INT_PTR)TRUE;
}
遍历子文件夹一般都用递归来做,给你个用递归实现的代码,如果不用递归做会比较麻烦。
#define MAX_PATH_TEST (1024)
void RecursionDir(char* pcDir, HANDLE hOutFile)
{
char strfile[MAX_PATH_TEST] = {};
char acFind[MAX_PATH_TEST] = {};
sprintf(acFind, "%s/*", pcDir);
WIN32_FIND_DATAA fileData;
HANDLE hfiandfile = FindFirstFileA(acFind,&fileData);
if (INVALID_HANDLE_VALUE == hfiandfile)
{
return;
}
do
{
if (0 == strcmp(fileData.cFileName, ".") || 0 == strcmp(fileData.cFileName, ".."))
{
continue;
}
ZeroMemory(strfile, sizeof(char)* MAX_PATH_TEST);
memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(fileData.cFileName));
strcat_s(strfile, "\r\n");
DWORD len = 0;
WriteFile(hOutFile, strfile, strlen(strfile), &len, nullptr);
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
char acChild[MAX_PATH_TEST] = {};
sprintf(acChild, "%s/%s", pcDir, fileData.cFileName);
RecursionDir(acChild, hOutFile);
}
} while (FindNextFileA(hfiandfile, &fileData));
FindClose(hfiandfile);
}
int main(){
WIN32_FIND_DATAA fileData;
char strfile[1024] = {};
HANDLE hflie = CreateFile(_T("遍历结果.txt"),
GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_NEW,FILE_ATTRIBUTE_NORMAL, nullptr);
RecursionDir("../遍历文件夹", hflie);
CloseHandle(hflie);
return 0;
}
bool iterFolder( HANDLE hflie, string strDir )
{
WIN32_FIND_DATAA FindData;
HANDLE hHandle;
char FilePathName[ MAX_PATH ];
char strfile[ 1024 ] = { };
strcpy_s( FilePathName, MAX_PATH, strDir.c_str() );
strcat_s( FilePathName, "\\*.*" );
hHandle = FindFirstFileA( FilePathName, &FindData );
if ( hHandle == INVALID_HANDLE_VALUE )
{
FindClose( hHandle );
return false;
}
// 构造路径
char FullPathName[ MAX_PATH ];
while ( ::FindNextFileA( hHandle, &FindData ) )
{
// 过虑.和..
if ( strcmp( FindData.cFileName, "." ) == 0 || strcmp( FindData.cFileName, ".." ) == 0 )
continue;
if ( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// 构造完整路径
sprintf( FullPathName, "%s\\%s", strDir.c_str(), FindData.cFileName );
string strfile = FindData.cFileName;
strfile += "\r\n";
DWORD len = 0;
WriteFile( hflie, strfile.c_str(), strfile.length(), &len, nullptr );
iterFolder( hflie, FullPathName ); // 遍历子目录
}
else
{
// 完整文件路径
sprintf( FullPathName, "%s\\%s", strDir.c_str(), FindData.cFileName );
string strfile = FindData.cFileName;
strfile += "\r\n";
DWORD len = 0;
WriteFile( hflie, strfile.c_str(), strfile.length(), &len, nullptr );
}
}
FindClose( hHandle );
return true;
}
int main()
{
WIN32_FIND_DATAA fileData;
char strfile[ 1024 ] = { };
HANDLE hflie = CreateFileA( ( "遍历结果.txt" ),
GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr );
iterFolder( hflie, "." );
CloseHandle( hflie );
return ( INT_PTR )TRUE;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: