Solved

Windows CopyFile function copies to wrong directory with wrong name

Posted on 2003-11-30
7
1,989 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
Comment Utility
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
Comment Utility
Try using '/' instead of '\' for the path.
0
 
LVL 5

Author Comment

by:arjanh
Comment Utility
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
Comment Utility
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
Comment Utility
User resolved; closed, 250 points refunded.
Netminder
Site Admin
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now