Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   IDFtp chequear si un directorio esta vacio (https://www.clubdelphi.com/foros/showthread.php?t=86715)

marilinspi 20-09-2014 16:18:56

IDFtp chequear si un directorio esta vacio
 
Hola necesito chequear que un directorio en la web esta vacío para así si subir mis archivo de lo contrario no... ej
Código Delphi [-]
if IDFtp(Prueba/Suc1)=vacío then
  subo archivos
else 
  no subo archivos
gracias desde ya...

ecfisa 21-09-2014 05:42:15

Hola marilinspi.

Un modo es:
Código Delphi [-]
...
uses IdFTP, IdFTPList;

function IsFtpDirEmpty(const aHost, aFolder, aUserName, aUserPass: string): Boolean;
var
  ts: TStrings;
begin
  with TidFTP.Create(nil) do
  try
    Result  := False;
    Host    := aHost;
    UserName:= aUserName;
    Password:= aUserPass;
    try
      Connect;
      if aFolder <> EmptyStr then
        ChangeDir(aFolder);
      ts:= TStringList.Create;
      try
        List(ts, '*.*', False);
        Result:= ts.Count = 0;
      finally
        ts.Free;
      end;
    except
      raise Exception.Create('Error al conectar con ' + aHost);
    end;
  finally
    Free;
  end;
end;

Ejemplo de uso:
Código Delphi [-]
procedure TForm1.btnCheckFilesClick(Sender: TObject);
var
  Host, Folder: string;
  Name, Pass: string;
begin
  Host  := 'ftp.swfwmd.state.fl.us';
  Folder:= ''; 
  Name  := 'anonymous';
  Pass  := 'nameless@domainless.com';
  if IsFtpDirEmpty(Host, Folder, Name, Pass) then
    ShowMessage('No existen archivos en la carpeta')
  else
    ShowMessage('Existen archivos en la carpeta');
end;

Saludos :)

nlsgarcia 22-09-2014 08:13:17

marilinspi,

Cita:

Empezado por marilinspi
...necesito chequear que un directorio en la web esta vacío...

:rolleyes:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses IdFTP, IdFTPList;

function IsFtpDirectoryEmpty(Host, Directory, UserName, Password : String): Integer;
var
   FTP: TIdFTP;
   FileList : TStringList;
   MsgUser : String;

begin

   try

      try

         FTP:= TIdFTP.create(nil);
         FTP.Host:= Host;
         FTP.Username:= UserName;
         FTP.Password:= Password;
         FTP.Connect(True, 5000);
         FTP.ChangeDir(Directory);
         FileList :=  TStringList.Create;
         FTP.List(FileList);

         if (FTP.DirectoryListing.Count = 2)
         and (FTP.DirectoryListing.Items[0].FileName = '.')
         and (FTP.DirectoryListing.Items[1].FileName = '..') then
            Result := 0
         else
         if FTP.DirectoryListing.Count = 0 then
            Result := 0
         else
            Result := 1;

      except

         MsgUser := Format('Error de I/O en Conexión al Directorio %s',[Directory]);
         MessageDlg(MsgUser,mtError,[mbOK],0);
         Result := -1;

      end;

   finally

      FTP.Quit;
      FTP.Free;

   end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   Host, Directory, Username, Password : String;

begin

   Host  := 'ftp.files.com';
   Directory := 'Files';
   UserName  := 'User';
   Password  := '1234';

   case IsFtpDirectoryEmpty(Host, Directory, Username, Password) of
      1 : MessageDlg(Format('El Directorio %s No Esta Empty',[Directory]),mtInformation,[mbOK],0);
      0 : MessageDlg(Format('El Directorio %s Esta Empty',[Directory]),mtError,[mbOK],0);
   end;

end;

end.
El código anterior en Delphi 7 con Indy 9 bajo Windows 7 Professional x32, permite verificar si un directorio tipo Windows o Unix a nivel local o en Internet contiene archivos o por el contrario esta vació.

Nota: El código del ejemplo fue probado en Internet y a nivel local con directorios tipo Unix y Windows.

Espero sea útil :)

Nelson.

marilinspi 22-09-2014 15:54:48

Gracias Nelson con tu ayuda lo resolví, simplificándolo un poco:
Código Delphi [-]
IdFTP1.ChangeDir( '/public_html/Prueba/Suc1/');
FileList :=  TStringList.Create;
IdFTP1.List(FileList);
if (IdFTP1.DirectoryListing.Count = 2)and (IdFTP1.DirectoryListing.Items[0].FileName = '.')and (IdFTP1.DirectoryListing.Items[1].FileName = '..') then
  Vacio:=True
  else
    if IdFTP1.DirectoryListing.Count = 0 then
      Vacio:=True
      else 
        Vacio:=False;

muchas gracias....


La franja horaria es GMT +2. Ahora son las 18:34:24.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi