Deltree

Delphi Pascal bölümünde yer alan bu konu SüKuN tarafından paylaşıldı.

  1. SüKuN

    SüKuN Harbi Aktif Üye

    Dostaki deltree komutu

    procedure TForm1.DelTree(const DirName: string);
    var
    { This procedure uses recursive calls to traverse directory
    structures, so pointers are used to keep stack usage to a
    minimum }
    SearchRec: ^TSearchRec;
    FileName: PString;
    GotOne: Integer;
    begin
    if trim(DirName)=" then exit;
    { Disable input/output checking }
    {$I-}
    { Allocate new TSearchRec record and a new FileName string }
    New(SearchRec);
    New(FileName);

    try
    GotOne := FindFirst(DirName + '\*.*', faAnyFile, SearchRec^);

    while (GotOne = 0) do
    begin
    with SearchRec^ do
    begin
    FileName^ := DirName + '\' + Name;

    { Is the current file a directory? }
    if ((Attr and faDirectory) = 0) then
    begin
    { The current file is not a directory. Delete it if
    it's not a system or volume ID file }
    if ((Attr and (faSysFile or faVolumeID)) = 0) then
    begin
    { Change the file's attributes if it's marked as
    read-only }
    if ((Attr and faReadOnly) <> 0) then
    FileSetAttr(FileName^,
    FileGetAttr(FileName^)
    and (not faReadOnly));

    DeleteFile(FileName^);
    end; { end if }

    end
    else if ((Name <> '.') and (Name <> '..')) then
    { The current file is a directory. Call ourselves
    recursively so that we traverse the sub directory
    structure }
    DelTree(FileName^);

    end; { end with }

    GotOne := FindNext(SearchRec^);
    end; { end while }

    FindClose(SearchRec^);
    RmDir(DirName);
    finally
    { Dispose of the memory allocate for the TSearchRec record and
    FileName string }
    Dispose(FileName);
    Dispose(SearchRec);
    end; { end try }

    { Re-enable input/output checking }
    {$I+}
    end;