Solved

Windows CopyFile function copies to wrong directory with wrong name

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

695 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