SHFileOperation - 仅文件副本

时间:2017-03-02 13:46:52

标签: c++

以下是一些将A驱动器的内容传输到C:\ test \ disk1的代码。该程序编译并运行正常。该程序创建一个名为“软盘驱动器(A)”的文件夹,其内容包含在其中。是否可以在没有文件夹的情况下从A驱动器复制文件?

#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");

}

1 个答案:

答案 0 :(得分:0)

以下是SHFILEOPSTRUCTW结构的参考: https://msdn.microsoft.com/en-us/library/windows/desktop/bb759795(v=vs.85).aspx

  

FOF_FILESONLY      如果指定了通配符文件名(*。*),则仅对文件(而不是文件夹)执行操作。

完整的解决方案将是这样的:

#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");

}