[delphi] clear temp

0

Witajcie, mam potrzebe wyczyszczenia folderu Temp w XP,
jak to zrobic najbardziej elegancko i sprawnie?
czy trzeba wyszukiwac pliki rekurencyjnie i usuwać czy moze istnieje jakas funkcja w delphi do tego? (d7 personal), pojawia sie tez problem zeby pliki ktorych nie mzona usunac ( sa np uzywane) mialyby byc pominiete, czekam na wskazówki - jestem beginner ;)

pozdrawiam

0

:) [soczek] , dla potomnych:

var
  D:tshfileopstruct;
begin

  FillChar(D, SizeOf(D), 0);
  D.wnd:=handle;
  D.wfunc:=FO_DELETE;  //operacja kasowanie katalogu lub pliku
  D.pfrom:= PChar('C:\aaa\*.*');   //co kasujemy?
  D.fflags:=fof_noconfirmation or fof_renameoncollision or FOF_NOERRORUI;
  D.fanyoperationsaborted:=true; //możliwość anulowania
  shfileoperation(D);  //wykonaj
end;

pozdrawiam

0

jednakze pojawia sie problem ;/
jak dam bezposrednio sciezko jaka znam do tempa to usuwa a jak dam zmienna pobraną to juz nie :

DZIAŁA:

var
  D:tshfileopstruct;
  TempDir: String;		
begin
  TempDir:= GetTempDir;
  FillChar(D, SizeOf(D), 0);
  D.wnd:=handle;
  D.wfunc:=FO_DELETE;  //operacja kasowanie katalogu lub pliku
  D.pfrom:= PChar('C:\Documents and Settings\konto\Ustawienia lokalne\Temp\*');  
  D.fflags:=fof_noconfirmation or fof_renameoncollision or FOF_NOERRORUI;
  D.fanyoperationsaborted:=true; //możliwość anulowania
  shfileoperation(D);  //wykonaj
end;

a to NIE DZIAŁA, why? :

function GetTempDir: string;
  begin;
    SetLength(Result, MAX_PATH);
    SetLength(Result, GetTempPath(MAX_PATH, (PChar(Result))));
  end;

///////////////////////////
var
  D:tshfileopstruct;
  TempDir: String;		
begin
  TempDir:= GetTempDir;
  FillChar(D, SizeOf(D), 0);
  D.wnd:=handle;
  D.wfunc:=FO_DELETE;  //operacja kasowanie katalogu lub pliku
  D.pfrom:= PChar(TempDir + '*');   // daje mi: 'C:\DOCUME~1\konto\USTAWI~1\Temp\*'
  D.fflags:=fof_noconfirmation or fof_renameoncollision or FOF_NOERRORUI;
  D.fanyoperationsaborted:=true; //możliwość anulowania
  shfileoperation(D);  //wykonaj
end;
///////////////////////////

ktos pomoze?

0

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 .

0

heh dzieki , byc moze to dziala, ale jak dla mnie za długie i do tego chce zeby to chodzilo na standardowych komponentach...
Prosze o przyjrzenie sie mojemu kodowi ktory podalem wyzej, pozdrawiam

0

może i długie, ale działa ;] ja to pisałem na potrzeby swojego programu

0

Witam. Nie analizowłem dokładnie ale na pierwszy rzut oka nie widzi mi się ta funkcja:

function GetTempDir: string;
  begin;
    SetLength(Result, MAX_PATH);
    SetLength(Result, GetTempPath(MAX_PATH, (PChar(Result))));
  end;

Czy jest zwracana przez nią wartość?
po mojemu to raczej :

function GetTempDir: string;
var S: string;
  begin;
    SetLength(S, MAX_PATH);
    GetTempPath(MAX_PATH, (PChar(S)));
    REesult:= S;
  end;
0
zibicoder napisał(a)

Witam. Nie analizowłem dokładnie ale na pierwszy rzut oka nie widzi mi się ta funkcja:

function GetTempDir: string;
  begin;
    SetLength(Result, MAX_PATH);
    SetLength(Result, GetTempPath(MAX_PATH, (PChar(Result))));
  end;

Czy jest zwracana przez nią wartość?
po mojemu to raczej :

function GetTempDir: string;
var S: string;
  begin;
    SetLength(S, MAX_PATH);
    GetTempPath(MAX_PATH, (PChar(S)));
    REesult:= S;
  end;

to samo zwraca przy czym ta druga funkcja jak sie sprawdzi wynik np przez showmessage generuje takjakby dluzyszy lancuch ze spacjami -> 'C:\Temp '
oco chodzi?

0

Do odczytu folderu TEMP takie pobieranie:

var
 Buffer: array[0..MAX_PATH-1] of Char;
 S : String;
begin
 SetString(S, Buffer, GetTempPath(SizeOf(Buffer), Buffer));
 ShowMessage(S);
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1