CHeighlund
Programmer
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.
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?
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?