0

In Delphi, I need to rename file that is in use by several users across the network. I can rename it from Windows explorer, but can not from Delphi using TFile.Move or RenameFile. In Delphi documentation I found that these functions are wrappers around Windows API MoveFile. File remains in the same drive and directory, so there is no problem with MoveFile constrains.

RenameFile(OldFileNameWithoutPath, NewFileNameWithoutPath);

My use case is installing new version of my program, I download new exe file from ftp, then to rename existing and running exe file, and finally rename exe file downloaded from ftp to standard name. From command promomt renaming part would be:

ren Izbori.exe IzboriOld.exe
ren IzboriNew.exe Izbori.exe
Daniel Vidić
  • 164
  • 2
  • 11
  • Just an observation based on your example: You are sure that the current directory is the one where the files are located? You explicitly state "`OldFileNameWithoutPath`" so in order for the `RenameFile` call to succeed, you need to `CHDIR(OldFileNamePath)` first. – HeartWare Feb 09 '21 at 08:29
  • 1
    You should look at the error code. It will tells you why rename failed. – fpiette Feb 09 '21 at 08:38
  • You cannot rename a exe that is opened by another user. I experienced this problem for years now at work myself! To overcome this you could use some trick with the file version in the filename of your main app. Your users only start a small app or batch file that looks for the main app with the highest version. So If you release a new version and put it in the network folder it gets used when your users start the “starter“ app the next time. – Delphi Coder Feb 09 '21 at 08:52
  • @DelphiCoder: From Explorer (and from command prompt), you _can_ rename an in-use .EXE file. You cannot, however, _delete_ an in-use .EXE file. – HeartWare Feb 09 '21 at 13:31

0 Answers0