Author: Jonas Bilinkevicius
Can anyone tell me how to delete several files using wininit.ini please?I've seen
an example somewhere that included the following :
[Rename]
NULL=C:\temp\readme.txt
Using the regular inifile calls, I cant use the above method for deleting several
files because each WriteString would overwrite previous "NULL=" entries. I'm unable
to find any info about using wininit.ini anywhere, there might be a [delete]
section for all I know.
Answer:
This will do the job:
1 2 procedure DeleteAtReboot(FileList: TStringList);
3 var4 SList: TStringList;
5 szContents: string;
6 i, SectionFoundIndex: Integer;
7 WinDir: array[0..MAX_PATH] of char;
8 WinFile: string;
9 begin10 if Win32Platform = VER_PLATFORM_WIN32_NT then11 begin12 {Use MoveFileEx}13 for i := 0 to FileList.count - 1 do14 MoveFileEx(PChar(FileList[i]), nil, MOVEFILE_DELAY_UNTIL_REBOOT);
15 end16 else17 begin18 GetWindowsDirectory(WinDir, MAX_PATH);
19 WinFile := IncludeTrailingBackslash(WinDir) + 'Wininit.ini';
20 SList := TStringList.Create;
21 try22 SectionFoundIndex := -1;
23 {Load it if it exists}24 if FileExists(WinFile) then25 SList.LoadFromFile(WinFile);
26 for i := 0 to SList.Count - 1 do27 begin28 szContents := uppercase(SList[i]);
29 if UpperCase(SList[i]) = '[RENAME]' then30 begin31 SectionFoundIndex := i;
32 break;
33 end;
34 end;
35 {Rename Section doesn't exist...}36 if SectionFoundIndex = -1 then37 SectionFoundIndex := SList.Add('[Rename]');
38 {Now Add our Files}39 for i := 0 to FileList.count - 1 do40 SList.Insert(SectionFoundIndex + 1, 'NUL=' + FileList[i]);
41 SList.SaveToFile(WinFile);
42 finally43 SList.Free;
44 end;
45 end;
46 end;