![]() |
#2
apull2021-04-23 00:02
![]() #ifdef _UNICODE #undef _UNICODE #endif // _UNICODE #ifdef UNICODE #undef UNICODE #endif // _UNICODE #include <Windows.h> #include <stdio.h> #include <string.h> #define ONE 1 #define BUFSIZE 1024 int ListDirectoryContents(const char *path); int main() { char path[BUFSIZE] = "D:\\data\\exam\\1"; ListDirectoryContents(path); //结束 return 0; } //查找path下的所有文件 int ListDirectoryContents(const char *path) { WIN32_FIND_DATA fileData = {0}; //文件属性 /* WIN32_FIND_DATA内容如下 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 ]; // 8.3格式文件名 } */ HANDLE h = NULL; //搜索句柄 int exit = 0; //退出代码 char newPath[BUFSIZE] = {0}; //搜索路径 char *addressOfLastCharacter = NULL; //字符串格式化 sprintf(newPath, "%s\\*.*", path); while (ONE) { if ((h = FindFirstFile(newPath, &fileData)) == INVALID_HANDLE_VALUE) //查找newPath里的文件,INVALID_HANDLE_VALUE表示出错 { printf("Path not found: [%s]\n", path); exit = 1; break; } do { if ((strcmp(fileData.cFileName, ".") != 0) && (strcmp(fileData.cFileName, "..") != 0)) //跳过当前和上级目录,避免重复查找 { sprintf(newPath, "%s\\%s", path, fileData.cFileName); //组合文件完整路径 if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) //如果是文件夹,则查找这个文件夹里的文件 { ListDirectoryContents(newPath); } else { printf("%s\n", fileData.cFileName); //如果是文件,则输出文件名 } } } while (FindNextFile(h, &fileData)); //查找下一个文件 break; } FindClose(h); //关闭搜索句柄 return exit; } |
#ifdef _UNICODE
#undef _UNICODE
#endif // _UNICODE
#ifdef UNICODE
#undef UNICODE
#endif // _UNICODE
#include<Windows.h>
#include<stdio.h>
#include<string.h>
#define ONE 1
#define BUFSIZE 1024
int ListDirectoryContents( const char *path );
int main()
{
char path[BUFSIZE] = "D:\\data\\exam\\1";
ListDirectoryContents( path );
//结束
return 0;
}
int ListDirectoryContents( const char *path )
{
WIN32_FIND_DATA fileData = { 0 };
HANDLE h = NULL;
int exit = 0;
char newPath[BUFSIZE] = {0};
char *addressOfLastCharacter = NULL;
//字符串格式化
sprintf( newPath, "%s\\*.*", path );
while(ONE)
{
if( ( h = FindFirstFile( newPath, &fileData ) ) == INVALID_HANDLE_VALUE )
{
printf( "Path not found: [%s]\n", path );
exit = 1;
break;
}
do
{
if( ( strcmp( fileData.cFileName, ".") != 0 ) && ( strcmp( fileData.cFileName, ".." ) != 0 ) )
{
sprintf( newPath, "%s\\%s", path, fileData.cFileName );
if( ( fileData.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) != 0 )
{
ListDirectoryContents( newPath );
}
else
{
printf( "%s\n", fileData.cFileName );
}
}
}
while( FindNextFile( h, &fileData ) );
break;
}
FindClose( h );
return exit;