Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/Delphi/File Management    [ Add a report in this area ]  
Report #:  79748   Status: Open
TDirectory.Copy doesn't support UNC paths as a destination
Project:  Delphi Build #:  14.0.3513.24210
Version:    14.0 Submitted By:   Roger Dunn
Report Type:  Basic functionality failure Date Reported:  11/23/2009 2:36:21 PM
Severity:    Commonly encountered problem Last Updated: 2/25/2013 10:34:48 PM
Platform:    All platforms Internal Tracking #:   273742
Resolution: None (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 11
Description
TDirectory.Copy doesn't support UNC paths as a destination
Steps to Reproduce:
1. Be connected to a network where you can access paths with UNC parts (such as \\Server6\System\Users\)
2. In Delphi 2010, make a new VCL Forms Application.
3. Place a button on the form.
4. Double-click it to make an OnClick event handler.
5. In the event handler's stub, paste the following code, and replace the parameters with real paths, making sure to end each path with a backslash:

TDirectory.Copy( '<a valid UNC path here>', '<a valid, local path here>' );

6. Put "uses IOUtils" in the implementation section of the unit.
7. Run the application.
8. Click Button1.
9. This works, as expected, and files are copied to the client.
10. Close the application.
11. Next, delete the files in your UNC path, keeping the copies in the client path.
12. In the OnClick event handler, switch the parameters to the Copy method so that the local path is first and the UNC path is second.
13. Run the application and click on Button1.  You should get this exception error message:

---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EDirectoryNotFoundException with message 'The drive cannot be found'.
---------------------------
Break   Continue   Help  
---------------------------

14. Of course the drive cannot be found--it is a UNC path.
15. Click Continue, then OK, and then exit the application.
16. In the OnClick event handler, change TDirectory to TFile, and in the first AND second parameters, specify an existing file name on the client.  Remember that because of step 11, the file does not exist on the UNC path.
17. Run the application and click Button1.  The single file is copied successfully.

I would like the TDirectory.Copy to accept UNC paths as destinations just like TFile.Copy does.
Workarounds
None
Attachment
None
Comments

Roger Dunn at 11/24/2009 3:19:53 PM -
The function TDirectory.CreateDirectory also fails when the parameter is a UNC path.  Oh, and TFile.GetLastWriteTime also fails, but with a different message.

Roman Kassebaum at 1/14/2011 7:07:06 AM -
The problem still occurs in DelphiXE. I fixed the code below and marked the fixes with R.K.:

{code:Delphi}
class procedure TDirectory.CheckCreateDirectoryParameters(const Path: string);
begin
  InternalCheckDirPathParam(Path, False);

{$IFDEF MSWINDOWS}
  { Windows-only: check if the drive of the given path actually exists }
//R.K.
  if TPath.IsDriveRooted(Path) and not TPath.DriveExists(TPath.DoGetPathRoot(Path)) then
//  if not TPath.DriveExists(TPath.DoGetPathRoot(Path)) then
    raise EDirectoryNotFoundException.CreateRes(@SDriveNotFound);
{$ENDIF}
end;
{code}

{code:Delphi}
class procedure TDirectory.Copy(const SourceDirName, DestDirName: string);
var
  PreCallback: TDirectoryWalkProc;
begin
  CheckCopyParameters(SourceDirName, DestDirName);

  PreCallback :=
    function (const Path: string; const FileInfo: TSearchRec): Boolean
    var
      CompletePath: string;
      CompleteSrc: string;
      CompleteDest: string;
    begin
      Result := True;

      // mirror each directory at the destination
      case FileInfo.Attr and SysUtils.faDirectory of
        // create a directory copy of the source
        SysUtils.faDirectory:
          if (FileInfo.Name <> TPath.FCCurrentDir) and (FileInfo.Name <> TPath.FCParentDir) then
          begin
            // the destination is the one given by DestDirName
            if SameFileName(SourceDirName, Path) then
              CompletePath := DestDirName
            // get the difference between Path and SourceDirName
            else
//R.K.
//              CompletePath := TPath.DoCombine(DestDirName,
//                StuffString(Path, 1, Length(SourceDirName) + Length(TPath.DirectorySeparatorChar), ''), False); // DO NOT LOCALIZE
              CompletePath := TPath.DoCombine(DestDirName,
                StuffString(Path, 1, Length(SourceDirName) + Length(TPath.DirectorySeparatorChar) - 1, ''), False); // DO NOT LOCALIZE
            CompletePath := TPath.DoCombine(CompletePath, FileInfo.Name, False);

            CreateDir(CompletePath);
          end;
        0: // move files from source to destination
            begin
              // determine the complete source and destination paths
              CompleteSrc := TPath.DoCombine(Path, FileInfo.Name, False);

              // the destination is the one given by DestDirName
              if SameFileName(SourceDirName, Path) then
                CompleteDest := DestDirName
              // get the difference between Path and SourceDirName
              else
//R.K.
//                CompleteDest := TPath.DoCombine(DestDirName,
//                  StuffString(Path, 1, Length(SourceDirName) + Length(TPath.DirectorySeparatorChar), ''), False); // DO NOT LOCALIZE
                CompleteDest := TPath.DoCombine(DestDirName,
                  StuffString(Path, 1, Length(SourceDirName) + Length(TPath.DirectorySeparatorChar) - 1, ''), False); // DO NOT LOCALIZE
              // add the file name to the destination
              CompleteDest := TPath.DoCombine(CompleteDest, FileInfo.Name, False);

              // copy the file from source to destination
              TFile.DoCopy(CompleteSrc, CompleteDest, True);
            end;
      end;
    end;

    // create the destination directory
    TDirectory.CreateDirectory(DestDirName);

    // move all directories and files
    WalkThroughDirectory(SourceDirName, '*', PreCallback, nil, True); // DO NOT LOCALIZE
end;

{code}

Natalie Vincent at 2/25/2013 6:45:40 PM -
This problem also exists in XE2, and it means that you cannot extract file using the zip library to a UNC path.

Server Response from: ETNACODE01