user21023
user21023

Reputation: 31

CopyFiles using IFileOperation (C++)

I want to copy multiple files using IFileOperation. Copy a single file is not a problem like this example: http://msdn.microsoft.com/en-us/library/windows/desktop/bb775761%28v=vs.85%29.aspx

My Problem is that I don't find a way to copy multiple files like *.txt. I have tried using SHCreateShellItemArrayFromIDLists, like this snippet:

    IShellItem *psiTo = NULL;
    HRESULT hr = SHCreateItemFromParsingName( csTarget, NULL, IID_PPV_ARGS(&psiTo) );
    LPCITEMIDLIST pidlFiles = ILCreateFromPath(csSource);
    UINT count = sizeof(pidlFiles);
    IShellItemArray* psiaFiles = NULL;
    hr = SHCreateShellItemArrayFromIDLists(count, &pidlFiles, &psiaFiles);
    hr = pfo->CopyItems(psiaFiles, psiTo);
    hr = pfo->PerformOperations();

A other way is to use SHCreateShellItemArray, like this:

LPCITEMIDLIST pidlParent = ILCreateFromPath(_T("C:\\"));
LPCITEMIDLIST pidlChild = ILCreateFromPath(_T("C:\\Temp\\*.txt"));
HRESULT hr = SHCreateShellItemArray(pidlParent, NULL, 1, &pidlChild, &psiaFiles);
hr = pfo->CopyItems(psiaFiles, psiTo);
hr = pfo->PerformOperations();

I tried different way but almost I get E_INVALIDIDARG or ERROR_PATH_NOT_FOUND. Whats wrong?

Upvotes: 2

Views: 3842

Answers (1)

MSalters
MSalters

Reputation: 180305

ILCreateFromPath doesn't take wildcards. Couldn't, since it returns a single PIDLIST_ABSOLUTE.

The easiest solution is to enumerate all files manually FindFirstFile(*.txt), call CopyItem on each result, and then call PerformOperations once at the end.

As usual, the older Windows functions are better. SHFileOperation is ten times simpler and more capable to boot: it does support wildcards directly.

Upvotes: 1

Related Questions