On Windows is there an interface for Copying Folders?

前端 未结 7 561
陌清茗
陌清茗 2021-01-18 06:41

I want to copy folder A and paste to desktop.

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

相关标签:
7条回答
  • 2021-01-18 07:05

    (assuming Windows)

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

    0 讨论(0)
  • 2021-01-18 07:16

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

    On Windows, the desktop is a special folder:

    // 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?
    
    0 讨论(0)
  • 2021-01-18 07:17

    Use this

    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;
    }
    
    0 讨论(0)
  • 2021-01-18 07:17

    Here's an example using SHFileOperation:

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

    Here's a quick hack without it:

    #import <stdlib.h>
    
    int main(int argc, char *argv[]) {
    
        system("robocopy \"C:\\my\\folder\" \"%userprofile%\\desktop\\\" /MIR");
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-18 07:17

    it works

    #include <iostream>
    
    int main()
    {
        system("xcopy C:\\Users\\Elmi\\Desktop\\AAAAAA\ C:\\Users\\Elmi\\Desktop\\b\ /e /i /h");
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-18 07:18

    Starting with Visual Studio 2015 you can use std::filesystem::copy which is even platform independent since it is available in implementations supporting >= 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();
        }
    }
    

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

    0 讨论(0)
提交回复
热议问题