Re: Loop through all the files and directories

Giganews Newsgroups
Subject: Re: Loop through all the files and directories
Posted by:  Remy Lebeau \(Indy Team\) (no.spam@no.spam.com)
Date: Thu, 17 May 2007

"Cyclone" <klo…@kloon.za.net> wrote in message
news:9B6986957126E340klo…@kloon.za.net...

> How would i loop through all the files in all the directories
> on a ftp without it being complecated?

The same way you do such looping for anything else when you don't know
how many items are present ahead of time - via a recursive loop, ie:

    procedure ProcessCurrentDirectory;
    var
        I: Integer;
        Directories: TStringList;
        Item: TIdFTPListItem;
        CurrentDir: String;
    begin
        FTP.List(nil);
        Directories := TStringList.Create;
        try
            for I := 0 to FTP.DirectoryListing.Count-1 do
            begin
                Item := FTP.DirectoryListing[I];
                case Item.ItemType of
                    ditDirectory:
                        Directories.Add(Item.FileName);
                    ditFile, ditSymbolicLink:
                        // process Item as needed ...
                end;
            end;
            for I := 0 to Directories.Count-1 do
            begin
                CurrentDir := FTP.RetrieveCurrentDir;
                FTP.ChangeDir(CurrentDir + Directories[I]);
                ProcessCurrentDirectory;
            end;
        finally
            Directories.Free;
        end;
    end;

    begin
        FTP.Connect;
        try
            ProcessCurrentDirectory;
        finally
            FTP.Connect;
        end;
    end;

Gambit

Replies

None

In response to

Loop through all the files and directories posted by Cyclone on Thu, 17 May 2007