Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Windows CopyFile function copies to wrong directory with wrong name

Posted on 2003-11-30
7
Medium Priority
?
2,028 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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