在 Windows 上有复制文件夹的界面吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4725115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:00:30  来源:igfitidea点击:

On Windows is there an interface for Copying Folders?

c++cwindows

提问by Elmi Ahmadov

I want to copy folder A and paste to desktop.

我想复制文件夹 A 并粘贴到桌面。

I am currently using C++ so preferably an OO interface if available.

我目前正在使用 C++,所以最好使用 OO 接口(如果可用)。

采纳答案by Elmi Ahmadov

it works

有用

#include <iostream>

int main()
{
    system("xcopy C:\Users\Elmi\Desktop\AAAAAA\ C:\Users\Elmi\Desktop\b\ /e /i /h");
    return 0;
}

回答by Daniel Gehriger

On Windows (Win32), you could use SHFileOperation, eg:

在 Windows (Win32) 上,您可以使用SHFileOperation,例如:

SHFILEOPSTRUCT s = { 0 };
s.hwnd = m_hWnd;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = "C:\target folder
bool CopyDirTo( const wstring& source_folder, const wstring& target_folder )
{
    wstring new_sf = source_folder + L"\*";
    WCHAR sf[MAX_PATH+1];
    WCHAR tf[MAX_PATH+1];

    wcscpy_s(sf, MAX_PATH, new_sf.c_str());
    wcscpy_s(tf, MAX_PATH, target_folder.c_str());

    sf[lstrlenW(sf)+1] = 0;
    tf[lstrlenW(tf)+1] = 0;

    SHFILEOPSTRUCTW s = { 0 };
    s.wFunc = FO_COPY;
    s.pTo = tf;
    s.pFrom = sf;
    s.fFlags = FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NO_UI;
    int res = SHFileOperationW( &s );

    return res == 0;
}
"; s.pFrom = "C:\source folder\*
#include <exception>
#include <experimental/filesystem> // C++-standard filesystem header file in VS15, VS17.
#include <iostream>
namespace fs = std::experimental::filesystem; // experimental for VS15, VS17.

/*! Copies all contents of path/to/source/directory to path/to/target/directory.
*/
int main()
{
    fs::path source = "path/to/source/directory";
    fs::path targetParent = "path/to/target";
    auto target = targetParent / source.filename(); // source.filename() returns "directory".

    try // If you want to avoid exception handling then use the error code overload of the following functions.
    {
        fs::create_directories(target); // Recursively create target directory if not existing.
        fs::copy(source, target, fs::copy_options::recursive);
    }
    catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
    {
        std::cout << e.what();
    }
}
"; SHFileOperation(&s);

回答by Behrouz.M

Use this

用这个

// String buffer for holding the path.
TCHAR strPath[ MAX_PATH ];

// Get the special folder path.
SHGetSpecialFolderPath(
    0,       // Hwnd
    strPath, // String buffer.
    CSIDL_DESKTOPDIRECTORY, // CSLID of folder
    FALSE ); // Create if doesn't exists?

回答by Roi Danton

Starting with Visual Studio 2015 you can use std::filesystem::copywhich is even platform independent since it is available in implementations supporting >= C++17.

从 Visual Studio 2015 开始,您std::filesystem::copy甚至可以使用独立于平台的版本,因为它可用于支持 >= C++17 的实现。

#import <stdlib.h>

int main(int argc, char *argv[]) {

    system("robocopy \"C:\my\folder\" \"%userprofile%\desktop\\" /MIR");
    return 0;
}

Change the behaviour of fs::copywith std::filesystem::copy_options. I've used std::filesystem::path::filenameto retrieve the source directory name without having to type it manually.

改变fs::copywith的行为std::filesystem::copy_options。我习惯于std::filesystem::path::filename检索源目录名称,而无需手动键入。

回答by Max

(assuming Windows)

(假设 Windows)

Use can use ShFileOperation (or IFileOperation::CopyItem on Vista). Max.

使用可以使用 ShFileOperation(或在 Vista 上使用 IFileOperation::CopyItem)。最大限度。

回答by Moo-Juice

For a platform agnostic solution, I'd suggest Boost::filesystem. That link is basically the reference material. There is a copy_filemethod that copies a file from one location to another.

对于平台无关的解决方案,我建议使用Boost::filesystem。该链接基本上是参考资料。有一种copy_file方法可以将文件从一个位置复制到另一个位置。

On Windows, the desktop is a special folder:

在 Windows 上,桌面是一个特殊的文件夹:

##代码##

回答by 2371

Here's an example using SHFileOperation:

下面是一个使用 SHFileOperation 的例子:

http://msdn.microsoft.com/en-us/library/bb776887%28VS.85%29.aspx#example

http://msdn.microsoft.com/en-us/library/bb776887%28VS.85%29.aspx#example

Here's a quick hack without it:

这是一个没有它的快速黑客:

##代码##