#include<windows.h>
#include<iostream>
#include<cstring>
using namespace std;
static long total = 0;
void ListFile(void)
{
HANDLE hSearch;
WIN32_FIND_DATA data;
hSearch = FindFirstFile("*", &data);
do{
if (strcmp(data.cFileName, ".") && strcmp(data.cFileName, ".."))
{
if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
cout<<"目录:"<<data.cFileName<<"/n";
SetCurrentDirectory(data.cFileName);
ListFile();
SetCurrentDirectory("..");
}
else
{
cout<<data.cFileName<<endl;
++total;
}
}
}while(FindNextFile(hSearch,&data));
FindClose(hSearch);
}
int main(int argc,char *argv[])
{
ListFile();
cout<<"总共 "<<total<<" 个文件"<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
static long total = 0;
void ListFile(void)
{
HANDLE hSearch;
WIN32_FIND_DATA data;
hSearch = FindFirstFile("*", &data);
do{
if (strcmp(data.cFileName, ".") && strcmp(data.cFileName, ".."))
{
if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
cout<<"目录:"<<data.cFileName<<"/n";
SetCurrentDirectory(data.cFileName);
ListFile();
SetCurrentDirectory("..");
}
else
{
cout<<data.cFileName<<endl;
++total;
}
}
}while(FindNextFile(hSearch,&data));
FindClose(hSearch);
}
int main(int argc,char *argv[])
{
ListFile();
cout<<"总共 "<<total<<" 个文件"<<endl;
system("pause");
return 0;
}
用的是递归的方法,还是比较简单的,改一下代码就可以实现查找指定文件。