remove all files of a directory

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
[email protected]
Posts: 14
Joined: Mon 21 Jun 2010 13:55

remove all files of a directory

Post by [email protected] » Wed 14 Mar 2012 15:56

Can you please send us a code example (possibly in C++ Builder) of how I can remove all files of a directory?
Thanks Fabio

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Thu 15 Mar 2012 13:54

Code: Select all

TStringList *Filelist;

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
  Filelist = new TStringList();
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::btDeleteClick(TObject *Sender)
{
  ScSSHClient->Connect();
  ScSFTPClient->Initialize();

  AnsiString Path = "."; //!
  TScSFTPFileHandle Handle;
  Filelist->Clear();
  Handle = ScSFTPClient->OpenDirectory(Path);
  try {
    do {
      ScSFTPClient->ReadDirectory(Handle);
    } while (!ScSFTPClient->EOF);

    for (int i = 0; i Count; i++) {
      AnsiString Filename = Path + "\" + Filelist->Strings[i];
      ScSFTPClient->RemoveFile(Filename);
    }
  }
  __finally {
    ScSFTPClient->CloseHandle(Handle);
  }
}

//---------------------------------------------------------------------------
void __fastcall TMainForm::ScSFTPClientDirectoryList(TObject *Sender,
      const AnsiString Path, const TBytes Handle,
      TScSFTPFileInfo *FileInfo, bool EOF)
{
  if ((FileInfo == NULL) || (FileInfo->Filename == ".") || (FileInfo->Filename == ".."))
    return;

  if ((FileInfo->Longname.Length() == 0) || (FileInfo->Longname.SubString(1,1) != "d"))
    Filelist->Add(FileInfo->Filename);
}
//---------------------------------------------------------------------------

[email protected]
Posts: 14
Joined: Mon 21 Jun 2010 13:55

Post by [email protected] » Fri 16 Mar 2012 08:39

Thanks for the answere, but when I double click to create the event OnDirctoryList, creates this method (C++ version XE2)
void __fastcall ScSFTPClient1DirectoryList(TObject *Sender, const UnicodeString Path, const TArray Handle, TScSFTPFileInfo *FileInfo, bool EOF);
That have arguments different from your example and over all, gives an error "Error in module Form1: Incorrect Method declaration in class Tform1". And if I try to change the arguments as yours the application doesn't stop to the event after the 'ReadDirectory'.
Why?

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Fri 16 Mar 2012 13:53

This is a known problem with XE.
As a workaround you should change TArray to TBytes and assign the event handler in code at run-time instead of assigning it at design-time.

Post Reply