Mega Code Archive

 
Categories / Delphi / Files
 

Delete files next time the computer is rebooted

Title: Delete files next time the computer is rebooted Question: This code shows you how to delete a file (whether it is currently in use or not) next time the computer is rebooted. Answer: ///////////////////////////////////////////////////////////////////////////////// //This function returns TRUE on ERROR_SUCCESS, or FALSE on other return values.// //For a descriptive error message call GetLastError when this function returns.// ///////////////////////////////////////////////////////////////////////////////// Function DeleteFileNextBoot(lpFileNameToDelete: PChar): Boolean; var WindowsDirs: array [0..MAX_PATH + 1] of Char; //holds Windows folder lpDir: array [0..MAX_PATH + 1] of Char; //holds the 8.3 format of lpFileNameToDelete VerPlatForm: TOSVersionInfoA; //holds current O.S version information StrLstDelte: TStrings; //holds the wininit.ini file of 95, 98 & ME begin //The default value of this function should be FALSE. To ensure //this it is best to assign the default value, rather than hope //the function has been zero-initialized correctly at run-time. Result := FALSE; //Gets the current OS, in terms of PLATFORM_NT or PLATFORM_WINDOWS. We //can then use this value to work out how we should delete a file next //time the computer is rebooted. NT does not use wininit.ini, there is //a API call MoveFileEx which works under NT, but doen't work under 95 ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm)); VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm); GetVersionEx(VerPlatForm); //This function is not designed for Windows 3.1 or 3.11 running WIN32 //so if that is detected to be the case, then exit this function. The //return value will be FALSE because we assigned the default (line 1) //The last error will be set to ERROR_NOT_SUPPORTED to indicate that //this function is unsupported on Windows 3.1 or 3.11 running "WIN32" if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then begin SetLastError(ERROR_NOT_SUPPORTED); Exit; end; if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then begin ////////////////////////// //WINDOWS NT, 2000 Code:// ////////////////////////// //Adds the file specified in lpFileNameToDelete to be processed //via the MoveFileEx. We extract the drive of the file to delte //so we can use it when making the NULL paramater. This is then //used to delete file file just after autochk has been executed //It does not matter if the file is in 8.3 or 255 char format.. if StrLen(PChar(ExtractFileDrive(lpFileNameToDelete))) Result := MoveFileEx(PChar(lpFileNameToDelete), PChar(ExtractFileDrive(lpFileNameToDelete) + '\NULL'), MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT) else Result := MoveFileEx(PChar(lpFileNameToDelete), PChar(ExtractFileDrive(lpFileNameToDelete) + 'NULL'), MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT); end else begin //////////////////////////// //WINDOWS 95, 98, ME Code:// //////////////////////////// //Create the stringlist which will hold the wininit.ini file, //and then obtain the WINDOWS folder on the current computer. StrLstDelte := TStringList.Create; GetWindowsDirectory(WindowsDirs, MAX_PATH + 1); //Checks for the existance of the wininit.ini file inside of //the WINDOWS folder, which is where the file must reside. //If the file is found, load the current contents into the //StringList StrLstDelte. if StrLen(WindowsDirs) 3 then begin if FileExists(WindowsDirs + '\wininit.ini') then StrLstDelte.LoadFromFile(WindowsDirs + '\wininit.ini'); end else begin if FileExists(WindowsDirs + 'wininit.ini') then StrLstDelte.LoadFromFile(WindowsDirs + 'wininit.ini'); end; //If there is no RENAME section inside of the wininit.ini file //then we need to add one. To determin if there is, use a find //function which returns the position of the line, or -1 if it //was unable to find the line that we searched for. ([rename]) if StrLstDelte.IndexOf('[rename]') = -1 then StrLstDelte.Add('[rename]'); //convert the filename and path into short 8.3 format. This is //done because wininit.exe can only process 8.3 filenames. The //long filename function has not yet been initialized. //Add the command string NUL={filename 8.3 format} to the file //making sure it is directly underneath the "[rename]" line. GetShortPathName(PChar(lpFileNameToDelete), lpDir, MAX_PATH + 1); StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL=' + lpDir); //Save the wininit.ini file. The file needs to be in the users //WINDOWS folder. Depending on the result of the GetWindowsDir //function, it may or may not be neccessary to add a '\' char. if StrLen(WindowsDirs) 3 then StrLstDelte.SaveToFile(WindowsDirs + '\wininit.ini') else StrLstDelte.SaveToFile(WindowsDirs + 'wininit.ini'); //The return value is now true. The file wininit.ini has //been successfully created, and is ready to be executed Result := TRUE; //Free the stringlist variable we created to hold the //wininit.ini file. StrLstDelte.Free; end; end;