Pomogę Ci gotowcem ;] Sam kiedyś pisałem taki program: DeleteAll
Źródło 3 funkcji, które są potrzebne:
procedure TfMain.DeleteFiles(Dir, Filter: string; User: boolean; ProgressAll: TProgressBar; lLabel: TJvListBox; IncludeSubdirs: Boolean = False);
var
Files, Tmp: TStringList;
i: integer;
begin
Files := TStringList.Create;
lLabel.Items.Insert(0, 'Wyszukiwanie plików/katalogów do usunięcia...');
if not User then
begin
FindFilesInDir(Dir, Filter, Files);
FindDirectories(Dir, Files);
end
else
if not IncludeSubdirs then
FindFilesInDir(Dir, Filter, Files, True)
else
begin
Tmp := TStringList.Create;
FindDirectories(Dir, Tmp);
for i := 0 to Tmp.Count - 1 do
FindFilesInDir(Tmp.Strings[i], Filter, Files, True);
Tmp.Free;
FindFilesInDir(Dir, Filter, Files, True);
FindDirectories(Dir, Files);
end;
if Files.Count = 1 then
ProgressAll.Max := Files.Count
else
if Files.Count > 0 then
ProgressAll.Max := Files.Count - 1;
if Files.Count = 0 then
lLabel.Items.Insert(0, 'Brak plików/katalogów do usunięcia...')
else
lLabel.Items.Insert(0, 'Rozpoczynam usuwanie ' + IntToStr(Files.Count) + ' plików/katalogów...');
for i := 0 to Files.Count - 1 do
begin
if Deleting = False then
Break;
if Files.Count = 1 then
ProgressAll.Position := i + 1
else
ProgressAll.Position := i;
if DirectoryExists(Files.Strings[i]) then
begin
if RemoveDir(Files.Strings[i]) then
lLabel.Items.Insert(0, Files.Strings[i] + ': Usunięto')
else
lLabel.Items.Insert(0, Files.Strings[i] + ': Błąd');
end
else
begin
FileSetAttr(Files.Strings[i], 0);
if DeleteFile(Files.Strings[i]) then
lLabel.Items.Insert(0, Files.Strings[i] + ': Usunięto')
else
lLabel.Items.Insert(0, Files.Strings[i] + ': Błąd');
end;
Application.ProcessMessages;
end;
Files.Free;
end;
procedure TfMain.FindFilesInDir(Dir, Filter: string; var slFiles: TStringList; User: Boolean = False);
var
SR: TSearchRec;
Files: TStringList;
begin
Files := TStringList.Create;
if FindFirst(Dir + '\' + Filter, faAnyFile, SR) = 0 then
repeat
Application.ProcessMessages;
if (SR.Name <> '.') and (SR.Name <> '..') then
if ((SR.Attr and faDirectory) = faDirectory) and (not User) then
FindFilesInDir(Dir + '\' + SR.Name, Filter, Files)
else
Files.Add(Dir + '\' + SR.Name);
until FindNext(SR) <> 0;
FindClose(SR);
slFiles.AddStrings(Files);
Files.Free;
end;
procedure TfMain.FindDirectories(Dir: string; var slDirs: TStringList);
var
SR: TSearchRec;
Dirs: TStringList;
begin
Dirs := TStringList.Create;
if FindFirst(Dir + '\*.*', faAnyFile, SR) = 0 then
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
if (SR.Attr and faDirectory) = faDirectory then
begin
FindDirectories(Dir + '\' + SR.Name, Dirs);
Dirs.Add(Dir + '\' + SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);
slDirs.AddStrings(Dirs);
Dirs.Free;
end;
Jeśli chcesz tylko usuwać zawartość katalogów TEMP (aktualnego usera i windows/temp) to parametr user pomijaj w wywoływaniu funkcji. Ścieżki do katalogów TEMP możesz uzyskać np z komponentu TJvComputerInfoEx.
A żeby nikt się nie czepiał (w tym ja) to ten kod jest na licencji: brak (rób z tym co chcesz ;])
PS: W razie pytań nt funkcji pisz ;]
EDIT: Do usuwania plików wywołujesz funkcje DeleteFiles, pozostałe 2 funkcje są używane przez DeleteFiles. Dla usunięcia wszystkich plików parametr Filter daj na .. Parametr IncludeSubDirs używany jest TYLKO gdy parametr USER = True. Przy katalogu TMP jako filtr zawsze ustawiaj .