Home » Blog
date 9.Mar.2025

■ Problems using IFileOperation for copying files


For quite some while now IFileOperation is touted as the recommended replacement for SHFileOperation, for doing all sorts of file management (copying, moving, deleting etc). It was introduced back in Windows Vista days, bringing fancier progress UI and interesting flags like FOFX_KEEPNEWERFILE (copies only changed files without overwrite confirmations). But perfect it is not; here is a list of IFileOperation problems I have discovered, sadly without any known solution

This is the kind of topic that makes an @$s out of chatGPT/copilot, just ask and you will see <g>

  1. No file filtering. SHFileOperation allowed wildcard filters like *.CPP to copy fewer files; IFileOperation doesn't offer any easy alternative — especially for recursive subfolder operations. The ideal solution would be to return S_FALSE from PreCopyItem, but sadly this aborts the entire file operation!?
     
  2. Buggy transfer to phones. When copying from your hard disk into MTP devices, all file dates are touched to the time of copying (now), wasting the original modification dates of the source files. The problem occurs only when the phone is the target (the opposite transfer direction works fine). The old IDropTarget::Drop method is the only way to get the modification dates correctly.
     
  3. SetProperties is broken. Apart from file operations, supposedly the interface can change file properties using SetProperties, except it doesn't! I couldn't make it work even in normal filesystem folders, never mind phones (which would have corrected the above problem, fixing the broken dates). Here's a code snippet that attempts to change file dates:

PROPVARIANT date;
date.vt = VT_FILETIME;
GetSystemTimeAsFileTime(&date.filetime);

PKA_FLAGS flag = 0;//PKA_SET;
CComPtr<IPropertyChangeArray> pArray;
hr = PSCreatePropertyChangeArray(&PKEY_DateModified, &flag, &date, 1, IID_PPV_ARGS(&pArray));

CComPtr<IFileOperation> spfo;
hr = spfo.CoCreateInstance(CLSID_FileOperation);

hr = spfo->ApplyPropertiesToItem(/*some shell item*/);
hr = spfo->SetProperties(pArray);
// asks elevation on PARENT folder (!) and even then it won't work
hr = spfo->PerformOperations();

So let's say we try to change the date of a file C:\TEMP\test.txt, it fails with this funny error message on the parent (!) folder C:\TEMP; even allowing to elevate, nothing happens. The same code on a phone file returns E_NOTIMPL.

Looks like the property portion wasn't tested at all... or perhaps they didn't intend to change basic properties like date modified? Not pretty anyway.

So given the date-destroying knack of IFileOperation and the lack of any means to fix MTP dates, you'd better refrain from using IFileOperation for copying to phones.

SetProperties error message

Post a comment on this topic »


©2002-2025 ZABKAT LTD, all rights reserved | Privacy policy | Sitemap