Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query regarding 'RenameFile()' procedure

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
I have a slight problem with the RenameFile procedure(function?) in Delphi, and I'm hoping someone can help me figure out what I'm doing wrong here.

I have a program I wrote that creates a .csv file for output; this file will be picked up by another program later. The .csv-generating program is run once a month. Prior to this, I have simply been allowing the 'current' run to overwrite the 'previous' run. However, due to a recent error (on my part; a change to the program that left part of the output incorrect), my boss has requested that I ensure that there is a one-month's record - that is, that the previous months file not be overwritten with the current one. As the .csv filename has to be hardcoded for the receiving program to catch it, I can't just make a quick change to allow for a different name there. I figured the RenameFile() would allow me to do what I wanted; the example in the Delphi Help pages is almost identical to what I need to do. However, every time I run it, I get the "can't do this" error message.

My code - mostly identical to the sample. Directory and file names (not extensions) have been changed, but that is the only difference between this and my own code.
Code:
  progPath:='C:\targetDir\filenm.csv';

  if FileExists(progPath) then
  begin
    BackupName := 'C:\targetDir\filenm.cpy';
    if not RenameFile(progPath, BackupName) then
      raise Exception.Create('Unable to create backup file.');
  end;

Can anyone suggest a reason why my program doesn't seem to be able to rename the file from *.csv to *.cpy? I know the program can write to that directory (it writes the .csv there, after all), so I don't see why I can't get it to rename the previous file. Any suggestions?
 
Is the file currently open by your or another process?

----------
Measurement is not management.
 
Not to my knowledge. The closest thing I can think of is that the program does attempt to open the file a few lines later (for rewriting purposes, hence the need to make the backup). Could that be what's causing the Rename to fail?
 
Okay, think I got it...you can't rename to an existing file...are you deleting the old backup?

----------
Measurement is not management.
 
ok,

I bet 100$ on the fact that Glenn is correct :)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I tested your code and it works perfectly. The answer to Glenn's question must be "true". Suggest looking further.
the program does attempt to open the file a few lines later
If indeed "false" then there's a good chance that Delphi's speed is causing it to be opened before the system has had a chance to shell out to the rename process.


And yes, it is an external call to Windows:
function MoveFile; external kernel32 name 'MoveFileA';

Try putting a sleep between the two or
Code:
while FileExists(progPath) do;
between the Rename and the Open.

Also check that BackupName does not already exist:
Code:
begin
  progPath:= 'C:\targetDir\filenm.csv';
  BackupName := 'C:\targetDir\filenm.cpy';
  if not FileExists(BackupName) then
    if FileExists(progPath) then
    begin
      if not RenameFile(progPath, BackupName) then
        raise Exception.Create('Unable to create backup file.');
    end
  else
    raise Exception.Create('BackupName already exists!')
end;


Roo
Delphi Rules!
 
@Glenn9999
Not yet, although I will be, so thanks for the heads-up on that issue. The problem was that I wasn't getting it to work even when there was no backup.

I'm going to try Roo's suggestion and see if that helps clear things up any.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top