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
Solved

Windows CopyFile function copies to wrong directory with wrong name

Posted on 2003-11-30
7
2,003 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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