How To Move Files That Are Currently in Use
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 NTWin32-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);
// Delete szSrcFile next time system is rebooted MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
Moving Files in Windows 95Windows 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:
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
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.
Article ID: 140570 - Last Review: 11/21/2006 15:50:00 - Revision: 4.3
- kbhowto kbapi kbprogramming kbkernbase kbfileio KB140570