Ranaming and copying files!

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!
kilocoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esoftbgCommented:
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
geobulCommented:
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
geobulCommented:
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
SaLzCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.