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 procedure DeleteAtReboot(FileList: TStringList);
2 var3 SList: TStringList;
4 szContents: string;
5 i, SectionFoundIndex: Integer;
6 WinDir: array[0..MAX_PATH] of char;
7 WinFile: string;
8 begin9 if Win32Platform = VER_PLATFORM_WIN32_NT then10 begin11 {Use MoveFileEx}12 for i := 0 to FileList.count - 1 do13 MoveFileEx(PChar(FileList[i]), nil, MOVEFILE_DELAY_UNTIL_REBOOT);
14 end15 else16 begin17 GetWindowsDirectory(WinDir, MAX_PATH);
18 WinFile := IncludeTrailingBackslash(WinDir) + 'Wininit.ini';
19 SList := TStringList.Create;
20 try21 SectionFoundIndex := -1;
22 {Load it if it exists}23 if FileExists(WinFile) then24 SList.LoadFromFile(WinFile);
25 for i := 0 to SList.Count - 1 do26 begin27 szContents := uppercase(SList[i]);
28 if UpperCase(SList[i]) = '[RENAME]' then29 begin30 SectionFoundIndex := i;
31 break;
32 end;
33 end;
34 {Rename Section doesn't exist...}35 if SectionFoundIndex = -1 then36 SectionFoundIndex := SList.Add('[Rename]');
37 {Now Add our Files}38 for i := 0 to FileList.count - 1 do39 SList.Insert(SectionFoundIndex + 1, 'NUL=' + FileList[i]);
40 SList.SaveToFile(WinFile);
41 finally42 SList.Free;
43 end;
44 end;
45 end;