Recipe 4.16. Replacing a File That Is in UseProblemYou want to replace a file that is currently locked by a process. SolutionUsing a command-line interfaceYou 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. ' ------ SCRIPT CONFIGURATION ------ ' 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( ) loop do while not objExec.StdOut.AtEndOfStream WScript.Echo objExec.StdOut.ReadLine( ) loop DiscussionEver 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 AlsoRecipe 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) |