• ARTICLES
SEARCH

How-To Geek

Easily Replace a Locked File Once it Becomes Unlocked

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:

image

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.

image

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

image

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

image 

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

Safeguards

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

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

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.
ECHO You can cancel replacing the Old File with the New File by closing now.
ECHO.

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=

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

   
:End
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.
   ECHO The file you right clicked on will be replaced with the other selected file.
   ECHO.
   ECHO Stopping without doing anything. Press any key to close.
   TIMEOUT /T 15
)
IF /I {%LogOffWhenDone%}=={Y} (
   ECHO.
   ECHO Option to Log Off when completed was selected.
   ECHO You will be logged off shortly.
   SHUTDOWN /L
)
ENDLOCAL

Links

Download ReplaceFile Script from SysadminGeek.com

Jason Faulkner is a developer and IT professional who never has a hot cup of coffee far away. Interact with him on Google+

  • Published 08/12/10

Comments (3)

  1. x3geek

    couldn’t help to notice a smiley icon in the script. :D after LogOffWhenDone=

  2. Pete

    One of the lines in your script has got messed up with a smily face:

    The DoReplace GOTO marker shows as oReplace

    SET /P LogOffWhenDone=
    :D oReplace
    DEL /F /Q %OldFile%

  3. pthubbard

    Alternatively, you could use a small utility like ‘Unlocker’ which works with all versions of Windows 32 & 64 bit. It’s a explorer context menu add-in that works very well from my long experience with it.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!