【问题标题】:On Windows is there an interface for Copying Folders?在 Windows 上是否有复制文件夹的界面?
【发布时间】:2011-06-11 03:21:34
【问题描述】:

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

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

【问题讨论】:

  • 您使用的是什么平台?视窗? Unix?这除了你的英语还可以。
  • 这是特定于平台的。你的目标是什么平台?

标签: c++ c windows


【解决方案1】:

在 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);

【讨论】:

    【解决方案2】:

    使用这个

    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;
    }
    

    【讨论】:

      【解决方案3】:

      从 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();
          }
      }
      

      std::filesystem::copy_options 更改fs::copy 的行为。我使用std::filesystem::path::filename 来检索源目录名称,而无需手动输入。

      【讨论】:

        【解决方案4】:

        (假设 Windows)

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

        【讨论】:

          【解决方案5】:

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

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

          // 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?
          

          【讨论】:

            【解决方案6】:

            这是一个使用 SHFileOperation 的示例:

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

            这是一个没有它的快速破解:

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

            【讨论】:

            • 他正在使用 windows :) 所以:system("robocopy C:\\my\\folder %HOMEPATH%\\Desktop\\folder /s /mir");
            • 它可以工作,但为什么不直接使用 Win32 API 呢?您正在有效地启动一个外部进程,然后该进程本身将使用相同的 Win32 API 执行操作。这对我来说似乎是一种非常复杂的处理方式,并且还增加了对外部程序的依赖。
            • 永远不要使用 system() 调用。有用于复制文件和文件夹的 windows api。请参阅 Max 的答案。
            【解决方案7】:

            有效

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

            【讨论】:

            • 这假设有xcopy。在 Windows Embedded 上不一定如此。此外,这与其说是“接口”,不如说是“外部程序”。另一个答案至少将这种方法称为 hack,确实如此。
            • 问题是 C++(可能是 Win32?)接口在 C++ 程序中复制文件夹; using system() 命令不是 C++ 接口,而是调用 O/S shell 命令。这里已经提供了可能的解决方案(Behrouz.M)
            猜你喜欢
            • 2012-12-14
            • 2021-04-16
            • 2014-07-28
            • 1970-01-01
            • 2020-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多