2017-03-02 71 views
0

以下是將A驅動器的內容傳輸到C:\ test \ disk1中的一些代碼。該程序編譯並運行良好。該程序創建一個名爲「軟盤驅動器(A)」的文件夾,內容包含在其中。是否有可能只從A驅動器複製文件而沒有文件夾?SHFileOperation - 僅文件複製

#include <Windows.h> 
#include <stdio.h> 

int main(int argc, char ** argv) 
{ 
    const wchar_t *const sourceFile = L"A:\\\0"; 
    const wchar_t *const outputFile = L"C:\\test\\disk1\0"; 

    SHFILEOPSTRUCTW fileOperation; 
    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW)); 

    fileOperation.wFunc = FO_COPY; 
    fileOperation.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_FILESONLY; 
    fileOperation.pFrom = sourceFile; 
    fileOperation.pTo = outputFile; 

    int result = SHFileOperationW(&fileOperation); 
    if (result != 0) 
    { 
     printf("SHFileOperation Failure: Error%u\n", result); 
     return 1; 
    } 

    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW)); 

    printf("Transfer complete\n"); 

} 

回答

0

這裏是SHFILEOPSTRUCTW結構的基準: https://msdn.microsoft.com/en-us/library/windows/desktop/bb759795(v=vs.85).aspx

FOF_FILESONLY Perform the operation only on files (not on folders) if a wildcard file name (*.*) is specified.

完整的解決方案將是這樣的:

#include <Windows.h> 
#include <stdio.h> 

int main(int argc, char ** argv) 
{ 
    const wchar_t *const sourceFile = L"A:\\*.*\0"; 
    const wchar_t *const outputFile = L"C:\\test\\disk1\0"; 

    SHFILEOPSTRUCTW fileOperation; 
    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW)); 

    fileOperation.wFunc = FO_COPY; 
    fileOperation.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_FILESONLY; 
    fileOperation.pFrom = sourceFile; 
    fileOperation.pTo = outputFile; 

    int result = SHFileOperationW(&fileOperation); 
    if (result != 0) 
    { 
     printf("SHFileOperation Failure: Error%u\n", result); 
     return 1; 
    } 

    memset(&fileOperation, 0, sizeof(SHFILEOPSTRUCTW)); 

    printf("Transfer complete\n"); 

} 
+0

乾杯Betontalpfa。編譯時出現此錯誤。 'C:\ C++ \ shfileop \ main.cpp | 16 |錯誤:期望數字常量之前的非限定id |'有什麼建議麼? –

+0

FOF_FILESONLY 如果指定了通配符文件名(\ *。*),則僅對文件(不在文件夾上)執行操作。 – betontalpfa

+1

是的,我想要從A驅動器傳輸所有文件。但它不會編譯。你能否詳細說明一下? –