Jump to content
Sign in to follow this  
FranzB

inverse function ForceDirectories ( myfolder)

Recommended Posts

I create a  working folder with  this  statement

workingpath  : String; 
myfolder : String;

/// example 
workingpath  :='\\myserver\username\options\process\'; 
myfolder := 'Runme\results\txt\';

ForceDirectories  ( workingpath  +  myfolder);

 

after completing my work I want to  delete  everything  in myfolder , keep  workingpath  untouched

Any good inverse  function ?

Edited by FranzB

Share this post


Link to post

Would the System.IOUtils TDirectory.Delete() work for you ?

It has the "Recursive" parameter option.

 

class procedure TDirectory.Delete(const Path: string; const Recursive: Boolean);
var
  PostCallback: TDirectoryWalkProc;
begin
  CheckDeleteParameters(Path, Recursive);

  if Recursive then
  begin
    PostCallback :=
      function (const Path: string; const FileInfo: TSearchRec): Boolean
      var
        CompletePath: string;
      begin
        Result := True;

        if (FileInfo.Name <> TPath.FCParentDir) and (FileInfo.Name <> TPath.FCCurrentDir) then
        begin
          CompletePath := TPath.DoCombine(Path, FileInfo.Name, False);

          // clear read-only, system and hidden attributes that can compromise
          // the deletion
{$IFDEF MSWINDOWS}
          FileSetAttr(CompletePath, System.SysUtils.faNormal);
{$ENDIF MSWINDOWS}

          case FileInfo.Attr and System.SysUtils.faDirectory of
            System.SysUtils.faDirectory: // remove empty directories
              RemoveDir(CompletePath);
            0: // remove files
              DeleteFile(CompletePath);
          end;
        end;
      end;

    // delete all the files and subdirectories
    WalkThroughDirectory(Path, '*', nil, PostCallback, Recursive); // DO NOT LOCALIZE
  end;

  // delete the directory itself
{$IFDEF MSWINDOWS}
  FileSetAttr(Path, System.SysUtils.faNormal);
{$ENDIF}
  RemoveDir(Path);
end;

 

BTW: It has also a CreateDirectory, which uses ForceDirectories(FullPath); and seems a little bit more reliable

class procedure TDirectory.CreateDirectory(const Path: string);
var
  FullPath: string;
begin
  FullPath := TPath.DoGetFullPath(Path);
  CheckCreateDirectoryParameters(FullPath);
  ForceDirectories(FullPath);
end;

 

Edited by Rollo62
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×