If you try to update certain Windows files (such as programs or word documents) while they are in use, you get the the standard “access denied, file is in use” error. While the reasoning behind this is obvious, it can be quite annoying if you need to update a small executable which is currently in use by another user. In these situations, you have, among others, the following choices, all of which take up your valuable time:

  • Track down and contact the users who are currently using the file, tell them to close out/save their work, etc. and then apply the update.
  • Don’t apply the update immediately and just remember to do it later when users are not in the system.
  • Schedule some utility to replace it at the next reboot.

Well, we have another solution available for you: a script you invoke via the Send To menu which does the following:

  1. Tries to delete the old file.
  2. If the old file is locked, the script waits 20 seconds. Go to step 1.
  3. If the old file is not locked, the old file is replaced with the new file. Go to step 4.
  4. Optionally log off once the process is done.

This way, you get just get the replace command in motion and the script takes care of the rest. This can help you avoid tracking down users or having to install unnecessary utilities on your system.

Setup and Usage

The script can be placed anywhere on your system. Then all you need to do is create a shortcut to it in your SendTo folder:

To start the replace process, select the old file and new file and then invoke the Send To option by right clicking on the old file/file to replace.

The script will display exactly what will happen and present you with the option to be logged off once the replace is completed.

The script will continuously try to delete the old file while waiting several seconds in between tries.


All you have to do is get the process running and whenever all your users are out, the file will be replaced.


The script has a couple of built in safeguards:

  • The old file and new file are clearly presented so you know exactly what will happen.
  • You can close the command window at any time to stop the action (of course, assuming the replace has not been performed already).
  • The script will ensure you have selected only two files when you invoke the Send To command. If you select, for example, 1 or 3 files you will receive a notice message and nothing will happen.

The Script

TITLE Replace Locked File
ECHO Replace Locked File
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com

SETLOCAL EnableExtensions

REM Validation.
SET Error=1
IF {%2}=={} (
   ECHO Two files must be selected to run the replace.
   GOTO End
IF NOT {%3}=={} (
   ECHO More than 2 files were selected so I am not sure what to do.
   GOTO End

SET Error=0
SET OldFile="%~f1"
SET NewFile="%~f2"
SET LogOffWhenDone=0

REM Show what will happen so you have a chance to cancel out.
ECHO Old File: %OldFile%
ECHO ---------
ECHO New File: %NewFile%
ECHO You can cancel replacing the Old File with the New File by closing now.

REM Log off prompt. If you do not want to see this, you can delete these lines.
ECHO Automatically log off once the replace process has completed?
ECHO Enter 'Y' to automatically log off or enter anything else to not.
SET /P LogOffWhenDone=

DEL /F /Q %OldFile%
IF NOT EXIST %OldFile% (
   MOVE %NewFile% %OldFile%
   ECHO File replaced successfully.
   GOTO End
ECHO The Old File is still locked. Waiting a few moments to try again.
GOTO DoReplace

IF {%Error%}=={1} (
   ECHO Instructions for use:
   ECHO 1. Select the two files in Windows Explorer.
   ECHO 2. Right click on the Old File and go Send To - Replace Locked File
   ECHO The file you right clicked on will be replaced with the other selected file.
   ECHO Stopping without doing anything. Press any key to close.
   TIMEOUT /T 15
IF /I {%LogOffWhenDone%}=={Y} (
   ECHO Option to Log Off when completed was selected.
   ECHO You will be logged off shortly.


Download ReplaceFile Script from SysadminGeek.com