#include <windows.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#define LEN 1024
// 深度优先递归遍历目录中所有的文件
BOOL DirectoryList(LPCSTR Path,char *ch)
{
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
char FilePathName[LEN];
// 构造路径
char FullPathName[LEN];
strcpy(FilePathName, Path);
strcat(FilePathName, "\\*.*");
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
cout<<"搜索失败!"<<endl;
return 0;
}
while(::FindNextFile(hError, &FindData))
{
// 过虑.和..
if (strcmp(FindData.cFileName, ".") == 0
|| strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
// 构造完整路径
wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
FileCount++;
// 输出本级的文件
cout<<FileCount<<" "<<FullPathName<<endl;
string fullpath=FullPathName;
string extension;
unsigned int loc=fullpath.rfind(".");
if(loc!=string::npos)
extension=fullpath.substr(loc,fullpath.length()-loc);
//文件扩展名,可根据需要自行添加
vector<string>extName;
{
extName.push_back(".avi");
extName.push_back(".AVI");
extName.push_back(".RMVB");
extName.push_back(".rmvb");
extName.push_back(".rm");
extName.push_back(".RM");
extName.push_back(".wmv");
extName.push_back(".WMV");
extName.push_back(".MP4");
extName.push_back(".mp4");
extName.push_back(".mov");
extName.push_back(".MOV");
extName.push_back(".3gp");
extName.push_back(".3GP");
extName.push_back(".MKV");
extName.push_back(".mkv");
extName.push_back(".flv");
extName.push_back(".FLV");
}
for(unsigned int i=0;i<extName.size();i++)
{
if(extension==extName[i])
{
char *cpy=new char[100];
strcpy(cpy,"copy ");
strcat(FullPathName," ");
strcat(cpy,FullPathName);
strcat(cpy,ch);
system(cpy);
}
}
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
cout<<"<Dir>";
DirectoryList(FullPathName,ch);
}
}
return 0;
}
//检查指定目录是否存在
bool CheckFolderExist(const string &strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false;
HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true;
}
FindClose(hFind);
return rValue;
}
int main()
{
cout<<"请输入要搜索的文件夹路径:";
char *path=new char[100];
cin>>path;
cout<<"请输入要复制到的文件夹路径:";
char *dir=new char[100];
cin>>dir;
string folder=dir;
//检查指定目录是否存在
if (!CheckFolderExist(folder))
{
char *mdfolder=new char[50];
strcpy(mdfolder,"md ");
strcat(mdfolder,dir);
cout<<"指定目录不存在..."<<endl;
cout<<"是否创建该目录?(Y/N)"<<endl;
string choice;
cin>>choice;
if(choice=="N"||choice=="n")
{
cout<<"程序即将退出..."<<endl;
system("pause");
exit(0);
}
else
{
system(mdfolder);
DirectoryList(path,dir);
}
}
else
DirectoryList(path,dir);
cout<<endl;
system("pause");
return 0;
}
|