4.16. Replacing a File That Is in Use


You want to replace a file that is currently locked by a process.


Using a command-line interface

You can use the inuse.exe command to replace a file that is locked by a process. You need to reboot after running the command for the change to take effect.

> inuse c:\foo.dll c:\windows\system32\foo.dll

In this example, the file c:\windows\system32\foo.dll will be replaced by c:\foo.dll after the system reboots.

Using VBScript

' This code executes the inuse 
command to replace a file.
' Modify the command string as necessary
strCmdString = "inuse.exe c:\foo.dll c:\windows\system32\foo.dll"
' ------ END CONFIGURATION ---------
set objWshShell = CreateObject("WScript.Shell")
set objExec = objWshShell.Exec(strCmdString)
do while not objExec.StdErr.AtEndOfStream
    WScript.Echo objExec.StdErr.ReadLine( )
do while not objExec.StdOut.AtEndOfStream
    WScript.Echo objExec.StdOut.ReadLine( )


Ever needed to replace a DLL or other file, but couldn't because the system said it was in use? With the inuse utility, you can replace files that are currently locked. Simply pass inuse the location of the new version of the file and the location of the currently locked file, and on reboot, the file will be overwritten. inuse works by setting a registry value that Windows looks at when booting up to determine if there are any pending file renames. For more information on the specific key and value, see MS KB 181345.

On Windows 2000, inuse is a Resource Kit tool and therefore not officially supported by Microsoft, so use it at your own risk. On Windows Server 2003, the command is part of the default installation and is supported. Also keep in mind that there is no "undo" function. So once you've overwritten a file, unless you made a copy of the original previously, you won't be able to revert back to it.

See Also

Recipe 4.20 for more on finding the process that has a file open, MS KB 181345 (How to replace in-use files at Windows restart), and MS KB 228930 (How to replace currently locked files with Inuse.exe)

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.