Sometimes Win32 applications need to delete, rename, or move files that arecurrently being used by the system. One common example is that setupprograms need to remove themselves from the user's hard disk when they arefinished setting up a software package. Sometimes, they also need to movedevice drivers that are currently being used by the system. Applicationsneed help from the operating system to delete or move these files.
Windows 95 and Windows NT each provide a unique method for helpingapplications to remove, replace, or rename files and directories that arein use. Although the two platforms differ in how they implement thesemethods, both share an overall strategy where the application specifieswhich files to process, and the system processes them when it reboots. Thisarticle explains how applications can use the method provided by eachWindows platform.
Moving Files in Windows NT
Win32-based applications running on Windows NT should use MoveFileEx() withthe MOVEFILE_DELAY_UNTIL_REBOOT flag to move, replace, or delete files anddirectories currently being used. The next time the system is rebooted, theWindows NT bootup program will move, replace, or delete the specified filesand directories.
To move or replace a file or directory that is in use, an application mustspecify both a source and destination path on the same volume (for example,drive C:). If the destination path is an existing file, it will beoverwritten. If the destination path is an existing directory, it will notbe overwritten and both the source and destination paths will remainunchanged. Here is an example call to move or replace a file or move adirectory:
// Move szSrcFile to szDstFile next time system is rebooted MoveFileEx(szSrcFile, szDstFile, MOVEFILE_DELAY_UNTIL_REBOOT);
To delete a file or directory, the application must set the destinationpath to NULL. If the source path is a directory, it will be removed only ifit is empty. Note that if you must use MoveFileEx() to remove files from adirectory, you must reboot the computer before you can call MoveFileEx() toremove the directory. Here is an example of how to delete a file or empty adirectory:
// Delete szSrcFile next time system is rebooted MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
Moving Files in Windows 95
Windows 95 does not implement MoveFileEx(), but does provide an alternateway for all Win32-based, 16-bit Windows-based, and MS-DOS-basedapplications to move, replace, or delete files (but not directories) thatare currently in use. This capability is implemented through the [rename]section of a file named Wininit.ini. If Wininit.ini is present in theWindows directory, Wininit.exe processes it when the system boots. OnceWininit.ini has been processed, Wininit.exe renames it to Wininit.bak.
The syntax of the [rename] section is:
DestinationFileName and SourceFileName must reside on the same volume andbe short (8.3) file names because Wininit.ini is processed before theprotected mode disk system is loaded, and long file names are onlyavailable when the protected mode disk system is running. Destination andsource files specified in Wininit.ini with long file names are ignored.
The [rename] section can have multiple lines with one file per line. Todelete a file, specify NUL as the DestinationFileName. Here are some entryexamples:
[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 causesExisting.txt to be moved to a new directory. The third causes Oldname.txtto be moved and renamed. The fourth causes an existing file to beoverwritten by Newfile.txt.
Applications should not use WritePrivateProfileString() to write entries tothe [rename] section because there can be multiple lines with the sameDestinationFileName, especially if DestinationFileName is "NUL." Instead,they should add entries by parsing Wininit.ini and appending the entries tothe end of the [rename] section.
NOTE: Always use a case-insensitive search to parse Wininit.ini because thetitle of the [rename] section and the file names inside it may have anycombination of uppercase and lowercase letters.
Applications that use Wininit.ini should check for its existence in theWindows directory. If Wininit.ini is present, then another application haswritten to it since the system was last restarted. Therefore, theapplication should open it and add entries to the [rename] section. IfWininit.ini isn't present, the application should create it and add to the[rename] section. Doing so ensures that entries from other applicationswon't be deleted accidentally by your application.
To undo a file rename operation before the system is rebooted, you mustremove the corresponding line from the [rename] section of the Wininit.inifile.