2011-01-18 17 views
5

フォルダAをコピーしてデスクトップに貼りたいです。Windowsにはフォルダのコピー用のインターフェイスがありますか?

私は現在、C++を使用していますので、可能であればOOインターフェイスを使用することをお勧めします。ここで

+1

あなたはどのプラットフォームを使用していますか? Windows? Unix?これはあなたの英語が大丈夫です。 – Benoit

+1

これはプラットフォーム固有です。あなたはどのプラットフォームをターゲットにしていますか? –

+0

@Benoit私はWindowsを使用しています。 –

答えて

-1

それはあなたがあるstd::filesystem::copyを使用することができます

#include <iostream> 

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

これは 'xcopy'があると仮定しています。必ずしもWindows Embeddedのケースではありません。また、これは「外部プログラム」のための「インターフェース」ではありません。もう1つの答えは、少なくともこのアプローチをハックと呼びます。 –

-2

はSHFileOperationを使用した例です:

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

は、ここでそれなしで迅速なハックです:

使用しShFileOperation(またはIFileOperationを使用することができます(Windowsのを想定)

#import <stdlib.h> 

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

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

彼は窓を使用しています:)それで 'system(" robocopy C:\\ my \\ folder%HOMEPATH%\\ Desktop \\ folder/s/mir ");' – Benoit

+0

@Benoitは動作してくれてありがとう... –

+0

It Win32 APIを直接使用しない理由は何ですか?あなたは効果的に外部プロセスを起動します。外部プロセスは、それと同じWin32 APIを使用してアクションを実行します。これは私にとっては非常に複雑な方法ですが、外部プログラムへの依存も追加します。 –

3

:: Vista上のCopyItem)。

13

の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); 
3

、私は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

使用この

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

は、Visual Studioとの2015年の作品> = C++をサポートしている実装で利用可能であるため、プラットフォームに依存しない場合もあります。

#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_optionsfs::copyの振る舞い。私はstd::filesystem::path::filenameを使用して、手動で入力することなくソースディレクトリ名を取得しました。

関連する問題