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

Elm*_*dov 6 c c++ windows

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

我目前正在使用C++,因此最好是OO接口(如果可用).

Dan*_*ger 15

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

SHFILEOPSTRUCT s = { 0 };
s.hwnd = m_hWnd;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = "C:\\target folder\0";
s.pFrom = "C:\\source folder\\*\0";
SHFileOperation(&s);
Run Code Online (Sandbox Code Playgroud)


Beh*_*z.M 7

用这个

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;
}
Run Code Online (Sandbox Code Playgroud)


Roi*_*ton 5

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

#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();
    }
}
Run Code Online (Sandbox Code Playgroud)

改变fs::copywith的行为std::filesystem::copy_options。我曾经使用std::filesystem::path::filename过无需手动输入即可检索源目录名称。


Elm*_*dov -1

有用

#include <iostream>

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

  • 这假设有“xcopy”。Windows Embedded 上的情况不一定如此。另外,这与其说是“接口”,不如说是“外部程序”。另一个答案至少称这种方法为黑客,事实确实如此。 (3认同)