Replace Files That Are Currently In Use

This page shows how to delete, rename, or move files that are currently being used by the system. Although the method to do this differs between Windows 9x and Windows NT/2K, both systems process the changes the next time the system is rebooted. To learn how to reboot the system programmatically, see my Shut Down and Restart Windows page.


(Last Updated 09/14/1999)

Windows NT/Windows 2K

The MoveFileEx function with the MOVEFILE_DELAY_UNTIL_REBOOT flag lets you move, replace, or delete files and directories currently being used. The next time the system is rebooted, the bootup program will move, replace, or delete the specified files and directories.

To move or replace a file or directory that is in use, your application must specify both a source and destination path on the same drive. If the destination path is an existing file, it will be overwritten. If the destination path is an existing directory, it will not be overwritten and both the source and destination paths will remain unchanged. For example, to move or replace a file or move a directory:

   '
   ' Move sSourceFile to sDestFile next time system is rebooted.
   '
   Dim sSourceFile As String
   Dim sDestFile As String

   Call MoveFileEx(sSourceFile, sDestFile, MOVEFILE_DELAY_UNTIL_REBOOT)

To delete a file or directory, your application must set the destination path to null. If the source path is a directory, it will be removed only if it is empty. If you have to use MoveFileEx to remove files from a directory, you must reboot the computer before you can call MoveFileEx to remove the directory. To delete a file or empty a directory:

   '
   ' Delete sSourceFile next time system is rebooted.
   '
   MoveFileEx(sSourceFile, vbNull, MOVEFILE_DELAY_UNTIL_REBOOT)

 

Windows 9x

To move, replace, or delete files (but not directories) that are currently in use you need to use the [rename] section of a file named Wininit.ini. If Wininit.ini is present in the Windows directory, Wininit.exe processes it when the system boots. Once Wininit.ini has been processed, Wininit.exe renames it to Wininit.bak.

The syntax of the [rename] section is:

   DestinationFileName=SourceFileName

DestinationFileName and SourceFileName must reside on the same drive and be short (8.3) file names. To convert a long path name to the equivalent short name, see my Convert Long Path Names to Short (8 Character) Names program.

The [rename] section can have multiple lines with one file per line. To delete a file, specify NUL as the DestinationFileName. For example:

   [rename]
   NUL=C:\TEMP.TXT
   C:\NEW_DIR\EXISTING.TXT=C:\EXISTING.TXT
   C:\NEW_DIR\NEWNAME.TXT=C:\OLDNAME.TXT
   C:\EXISTING.TXT=C:\TEMP\NEWFILE.TXT

The first line causes Temp.txt to be deleted. The second causes Existing.txt to be moved to a new directory. The third causes Oldname.txt to be moved and renamed. The fourth causes an existing file to be overwritten by Newfile.txt.

You should always check if Wininit.ini exists before using it. If it does, then another application has written to it since the system was last restarted. Your application should then open it and append entries to the [rename] section. If Wininit.ini isn't present, your application should create it and add to the [rename] section.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page