#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include<io.h>
#include<iostream>
using namespace std;
void getAllFiles(string path, list<string>& files);
void copyAllFiles(list<string>& files, string dirNewPath);
string getWorkPath();
int main(int argc, char* argv[])
{
string exePath = getWorkPath();
list<string> files;
string oldPath = exePath;
getAllFiles(oldPath.append("\\test"),files);
copyAllFiles(files, exePath);
return 0;
}
void getAllFiles(string path, list<string>& files)
{
//文件句柄
//long hFile = 0; //win7
intptr_t hFile = 0; //win10
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
// "\\*"是指读取文件夹下的所有类型的文件,若想读取特定类型的文件,以png为例,则用“\\*.png”
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR))
{
string name = fileinfo.name;
if (name.find(".",0)== string::npos)
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(path + "\\" + fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void copyAllFiles(list<string>& files, string dirNewPath)
{
for (auto obj: files)
{
string filePath = obj;
string fileName = filePath.substr(filePath.find_last_of("\\")+1);
string newfilePath = dirNewPath;
newfilePath.append("\\").append(fileName);
CopyFile(filePath.c_str(), newfilePath.c_str(), true);//false代表覆盖,true不覆盖
}
}
string getWorkPath()
{
char szBuf[256];
char *p;
GetModuleFileName(NULL, szBuf, sizeof(szBuf));//拿到全部路径
p = szBuf;
string exePath = p;
exePath = exePath.substr(0, exePath.find_last_of("\\"));
return exePath;
}
网友评论