?
Solved

Ranaming and copying files!

Posted on 2004-11-04
8
Medium Priority
?
136 Views
Last Modified: 2010-04-05
renamefile() doesnt work, or i can't get the arguments right. I need to copy a jpg (not that it matters) and then rename it. I need a couple of functions that WORK!
0
Comment
Question by:kilocoder
5 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 172 total points
ID: 12498783
Odd...
The RenameFIle function just uses the MoveFile api behind the scenes, and should only fail if:

1.) the file to rename TO already exists
2.) you attempt to rename directories across different drives.

You could try the following to figure out where the problem is

if not(MoveFIle(PChar(FIleNameFrom), PChar(FileNameTo))) then
  RaiseLastWin32Error; // Will raise error message which should hopefully narrow things down

Regards,
Russell
0
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 164 total points
ID: 12499173
uses
  ...., IdGlobal;

function  CopyAndRename(S, T: string): Boolean; //  S is full source file name;  T is full target file name;
begin
  Result := CopyFileTo(S, T); // for example CopyFileTo('C:\PHOTOGRAPHS\JPEG\E2002_04.jpg', 'E:\UPLOADS\E4.JPG');
end;
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 164 total points
ID: 12501936
Hi,

I've been using the following function to communicate with other applications via files:

// Moves a file using the original filename as a temporary new name
// returns:
// 0 - success
// 1 - CopyFile failed
// 2 - Remote rename failed
// 3 - Local delete failed
// 99 - unknown error (perhaps wrong ToFile parameter)
function GeoMoveFile(FromFile,ToFile: string) : integer;
begin
  result := 0;
  try
    if not Windows.CopyFile(PChar(FromFile), PChar(ExtractFilePath(ToFile) + ExtractFileName(FromFile)), false) then begin
      result := 1;
      exit;
    end;
    if not RenameFile(ExtractFilePath(ToFile)+ExtractFileName(FromFile),ToFile) then begin
      result := 2;
      exit;
    end;
    if not DeleteFile(FromFile) then result := 3;
  except
    result := 99;
  end;
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
var
  res: integer;
begin
  res := GeoMoveFile('c:\temp\print.txt', 'c:\tot.txt');
  if res > 0 then begin
    // handle the error here
    ShowMessage('Error '+IntToStr(res));
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 12501950
Perhaps you don't want to delete the original file and must remove the line:
 if not DeleteFile(FromFile) then result := 3;
from the above code.
0
 
LVL 2

Expert Comment

by:SaLz
ID: 12708068
ok there are 2 ways to do this, copy or move the file which can change the files name.

//MoveFile, can move the file to a new location with a new file name or to simple rename the file at that selected location.
moveFile(pchar('C:\Testers\1.txt'),pchar('C:\Testers\1ssss.txt'));

//CopyFile, can copy the file to a new location with a new file name.
copyFile(pchar('C:\Testers\1.txt'),pchar('C:\Testers\2.txt'),true);



 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

850 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