How to remove folders from a directory

Asked

Viewed 345 times

0

I have a project in Delphi 2010 in which I use to compress and create backups Winrar, through the following code I can compress the following folder teste3:

AFile := 'C:\teste1\teste2\teste3';
LocateFile := 'C:\Users\Desktop\BACKUPS\'+ FormatDateTime('yyyy-mm-dd', Now) +'.rar';
winexec(PAnsiChar(AnsiString('"C:\Program Files\WinRAR\WinRAR.exe" a '+ LocateFile +' "'+ AFile + '"')), SW_HIDE);

But the compressed file is not showing as intended with the folder only teste3 I leave here the example of what I want.

This is the directory of the folder teste3:

C: teste1 teste2 teste3

Structure:

c:
  teste1
    teste2
      teste3
        exemplo1.txt
        exemplo2.txt
        exemplo3.txt
      ficheiro1.txt
    ficheiro1.txt

When compressed inside the .rar have:

ficheiro.rar
  teste1
    teste2
      teste3
        exemplo1.txt
        exemplo2.txt
        exemplo3.txt

What do I want:

ficheiro.rar
  teste3
    exemplo1.txt
    exemplo2.txt
    exemplo3.txt

If anyone can contribute anything thank you.

  • see this http://www.swissdelphicenter.ch/en/showcode.php?id=2413 http://docwiki.embarcadero.com/CodeExamples/Berlin/en/DirectoryOperations_(Delphi)

  • maybe I did not explain myself well in the question, but those two folders have files inside them, but when I compress the folder "teste3" that is inside them they are also compressed things that I did not want and without files

  • I have already made an adjustment to my question to try to be more explicit what I need

1 answer

1


You can use the option -ep1, so the base folder will not be added.

In your code use like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  AFile: string;
  LocateFile: string;
begin
  AFile := 'C:\teste1\teste2\teste3';
  LocateFile := 'C:\Users\Desktop\BACKUPS\'+ FormatDateTime('yyyy-mm-dd', Now) +'.rar';

  winexec(PAnsiChar(AnsiString('"C:\Program Files\WinRAR\WinRAR.exe" -ep1 a '+ LocateFile +' "'+ AFile + '"')), SW_HIDE);
end;

To ignore empty folders, use the option -ed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.