See steps below how to reproduce. I use Delphi 10.1 Berlin and Windows 10 and compile to win32.
- Create a new VCL Forms Application
- Place a TTimer and a TMemo on the form
- Set the timer's Interval to 10 ms
- Put this code in the OnTimer event:
if FileExists('named.txt') then
begin
Memo1.Lines.Add('named.txt exists');
DeleteFile('renamed.txt'); //delete if it exists
if RenameFile('named.txt', 'renamed.txt') then
Memo1.Lines.Add(' renamed OK')
else
Memo1.Lines.Add(' rename failed with error : '+ IntToStr(GetLastError));
end;
Run the program
Create a file named.txt
TMemo output shows:
named.txt exists renamed OK
- Now rename the file renamed.txt back to named.txt in the explorer.
TMemo output now shows:
named.txt exists renamed OK named.txt exists renamed OK
But there will come an error message showing "File or folder does not exists". Why?
(Renamefile returns OK).
Setting the timer's Interval to e.g 500 ms seems to be ok (no error message).
Here is the message (in Swedish):
I even copied the exe-file to another PC with the same result:
I was experiencing the same problem,
first: it doesnt seem to have something to do with your code.
From what i figured out the error is an Windows error and occurs, when you try to rename a file again before the first rename process(form Windows) has properly finished, so the delphi rename process may have finished and your code continues(and starts another rename) but the windows rename isnt properly finished yet.
The same problem happens, when you use diffrent programming languages for example with an batch file:
you get the same error message.
To fix your problem the only thing i can think of, is to increse the timer delay like you suggested in your post.
I hope, this was any help and I am very sory i coulndt solve your problem