Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 May : SHFileOperation, SHNameMapping

www.cryer.info
Managed Newsgroup Archive

SHFileOperation, SHNameMapping

Subject:SHFileOperation, SHNameMapping
Posted by:"DRS" (drs@removethis.ihug.com.au)
Date:Wed, 23 May 2007 18:52:38

D6.02, XP Pro SP2.

I've been successfully using Wayne Niddrey's SHFileOp wrapper for
SHFileOperation for a while.  However, in the event of a collision when
copying folders I need to recover the new path (ie, "Copy of Foo").  To that
end I added the FOF_WANTMAPPINGHANDLE flag to SHFileop and wrapped some code
I got from a Borland newsgroup to get the remapped path name thus:

  function GetNameMappings(aFileOpStruct: TSHFileOpStruct): string;
  type
    TNameMappings = record
      countofnames: integer;
      namemaps: array[0..maxint div 16] of PSHNameMapping;
    end;
    PNameMappings = ^TNameMappings;

  var
    p: PNameMappings;
    pnm: PSHNameMapping;
    ncount: integer;
    s: string;

  begin
    Result := '';
    p := PNameMappings(aFileOpStruct.hNameMappings);
    if p = nil then Exit;
    ncount := p.countofnames;
    if ncount = 0 then exit;
    try
      while (ncount > 0) do
        begin
          dec(ncount);
          pnm := p.namemaps[ncount];
          setlength(s, pnm.cchNewPath);
          strlcopy(pchar(s), pnm.pszNewPath, length(s));
          Result := s;
        end;
    finally
      SHFreeNameMappings(Cardinal(aFileOpStruct.hNameMappings));
    end;
  end;

Assuming the source folder is D:\A\temp1\foo and the destination folder is
D:\A\temp2 and D:\A\temp2\foo already exists then the copy will result will
in D:\A\temp2\Copy of foo.  In this example I have tested that ncount = 1
and pnm.cchNewPath = 22, exactly as I would expect.  However, the value for
S is always 'D', never the full path string.

Replies:

www.cryer.info
Managed Newsgroup Archive