Quick Links

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:

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

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.

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

Download ReplaceFile Script from SysadminGeek.com