我无法用C+;复制文件夹中的所有文件+; 我想在C++中编写一个程序,在其中复制文件夹中的所有文件并粘贴到另一个文件夹中。目前,我只管理了一个文件 #include <iostream> #include <windows.h> using namespace std; int main (int argc, char *argv[]) { CopyFile ("C:\\Program Files (x86)\\WinRAR\\Rar.txt","C:\\Users\\mypc\\Desktop\\don't touch\\project\\prova", TRUE); #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { CopyFile(“C:\\ProgramFiles(x86)\\WinRAR\\Rar.txt”,“C:\\Users\\mypc\\Desktop\\don't touch\\project\\prova”,TRUE);

我无法用C+;复制文件夹中的所有文件+; 我想在C++中编写一个程序,在其中复制文件夹中的所有文件并粘贴到另一个文件夹中。目前,我只管理了一个文件 #include <iostream> #include <windows.h> using namespace std; int main (int argc, char *argv[]) { CopyFile ("C:\\Program Files (x86)\\WinRAR\\Rar.txt","C:\\Users\\mypc\\Desktop\\don't touch\\project\\prova", TRUE); #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { CopyFile(“C:\\ProgramFiles(x86)\\WinRAR\\Rar.txt”,“C:\\Users\\mypc\\Desktop\\don't touch\\project\\prova”,TRUE);,c++,windows,directory,dev-c++,file-copying,C++,Windows,Directory,Dev C++,File Copying,正如其中一条评论所建议的,CopyFile一次只复制一个文件。一个选项是循环目录并复制文件。使用文件系统(可以阅读),允许我们递归地打开目录,复制该目录、文件和目录,直到所有内容都被复制。此外,我没有检查用户输入的参数,所以不要忘记,如果它对您很重要的话 # include <string> # include <filesystem> using namespace std; namespace fs = std::experimental::filesystem;

正如其中一条评论所建议的,CopyFile一次只复制一个文件。一个选项是循环目录并复制文件。使用文件系统(可以阅读),允许我们递归地打开目录,复制该目录、文件和目录,直到所有内容都被复制。此外,我没有检查用户输入的参数,所以不要忘记,如果它对您很重要的话

# include <string>
# include <filesystem> 

using namespace std;
namespace fs = std::experimental::filesystem;
//namespace fs = std::filesystem; // my version of vs does not support this so used experimental

void rmvPath(string &, string &);

int main(int argc, char **argv)
{
    /* verify input here*/

    string startingDir = argv[1]; // argv[1] is from dir
    string endDir = argv[2]; // argv[2] is to dir

    // create dir if doesn't exist
    fs::path dir = endDir;
    fs::create_directory(dir);

    // loop through directory
    for (auto& p : fs::recursive_directory_iterator(startingDir))
    {
        // convert path to string
        fs::path path = p.path();
        string pathString = path.string();

        // remove starting dir from path
        rmvPath(startingDir, pathString);

        // copy file
        fs::path newPath = endDir + pathString;
        fs::path oldPath = startingDir + pathString;


        try {
            // create file
            fs::copy_file(oldPath, newPath, fs::copy_options::overwrite_existing);
        }
        catch (fs::filesystem_error& e) {
            // create dir
            fs::create_directory(newPath);
        }
    }

    return 0;
}


void rmvPath(string &startingPath, string &fullPath) 
{
    // look for starting path in the fullpath
    int index = fullPath.find(startingPath);

    if (index != string::npos)
    {
        fullPath = fullPath.erase(0, startingPath.length());
    }
}
#包括
#包括
使用名称空间std;
命名空间fs=std::实验::文件系统;
//命名空间fs=std::filesystem;//我的vs版本不支持这样使用
void rmvPath(string&,string&);
int main(int argc,字符**argv)
{
/*在此处验证输入*/
字符串startingDir=argv[1];//argv[1]来自dir
字符串endDir=argv[2];//argv[2]是指向dir的
//如果不存在,则创建目录
fs::path dir=endDir;
fs::创建_目录(dir);
//循环浏览目录
for(auto&p:fs::recursive\u directory\u迭代器(startingDir))
{
//将路径转换为字符串
fs::path path=p.path();
string pathString=path.string();
//从路径中删除起始目录
rmvPath(startingDir,路径字符串);
//复制文件
fs::path newPath=endDir+pathString;
fs::path oldPath=startingDir+pathString;
试一试{
//创建文件
fs::copy_file(oldPath、newPath、fs::copy_options::overwrite_existing);
}
捕获(fs::文件系统错误&e){
//创建目录
fs::创建_目录(newPath);
}
}
返回0;
}
void rmvPath(字符串和起始路径、字符串和完整路径)
{
//在完整路径中查找起始路径
int index=fullPath.find(起始路径);
如果(索引!=string::npos)
{
fullPath=fullPath.erase(0,startingPath.length());
}
}

CopyFile()
只复制一个文件。要复制多个文件,需要循环所有文件,并为每个文件调用
CopyFile()
。查找函数
FindFirstFile()
FindNextFile()
以获得实现该循环的选项,以及
GetFileAttributes()
SetFileAttributes()
如果需要复制文件属性(例如保护)。请记住,目录包含目录-要复制目录,必须递归地循环目录中的所有文件。或者,如
SHFileOperation()
,那么您就不需要循环了,您可以使用通配符。请检查,它应该适用于任何系统,而不仅仅是Windows。