?
Solved

Windows CopyFile function copies to wrong directory with wrong name

Posted on 2003-11-30
7
Medium Priority
?
2,021 Views
Last Modified: 2013-12-03
Hi,

I got an issue with the Windows API function CopyFile which I'm calling from Delphi. Files aren't renamed correctly!

I have this code:
  {$IFDEF DEBUG} fDebug.Log( 'D', 'Renaming ''' + PChar(filename) + ''' to ''' + PChar(NewFileName) + '''...' ); {$ENDIF}
  if Windows.CopyFile( PChar( filename ), PChar( NewFileName ), False ) then

Which prints this in the debug file:
30-Nov-2003 21:55:01.985 [D] Renaming 'E:\Documents and Settings\Arjan\My Documents\cvswork\NExt\Chunks\Test.jpg' to 'E:\Documents and Settings\Arjan\My Documents\cvswork\NExt\Downloads\Test.jpg'...


However, the function returns False (meaning the copy failed) and the file is now called 'Chunks Test.jpg' and is located in 'E:\Documents and Settings\Arjan\My Documents\cvswork\NExt\'.


What can I do to move the file to the correct directory? I tried to give the directory as destination, but with the same result.


Thanks,
Arjan
0
Comment
Question by:arjanh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:bill553030
ID: 9853344
Have you called GetLastError()?

This may give you a clearer idea of what is causing it to fail.
0
 
LVL 2

Expert Comment

by:MattWare
ID: 9876737
Try using '/' instead of '\' for the path.
0
 
LVL 5

Author Comment

by:arjanh
ID: 9933991
Sorry for taking so long to get back. Problem still exists.
@bill553030: Error code returned by GetLastError is 123 - ERROR_INVALID_NAME
I don't see anything invalid however, and find it very weird that the file gets written 1 directory level higher!

@MattWare: I'm at windows, so I think '\' should do the trick.
0
 
LVL 5

Author Comment

by:arjanh
ID: 9937115
I am now using this workaround, and this works fine where the CopyFile fails:

var
  ToFile, FromFile: TFileStream;

FromFile := TFileStream.Create(filename, fmOpenRead);
ToFile := TFileStream.Create(NewFileName, fmCreate);

ToFile.CopyFrom( FromFile, 0 );

ToFile.Free;
FromFile.Free;
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 10819810
User resolved; closed, 250 points refunded.
Netminder
Site Admin
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question