Recent

Author Topic: FTP directory list  (Read 10513 times)

christensen

  • Full Member
  • ***
  • Posts: 126
FTP directory list
« on: January 18, 2016, 05:33:03 pm »
Hello,

I'm trying to list the directories from FTP server but with no luck.

Code: Pascal  [Select]
  1. function TfrmUpload.SendFTP(Host, Username, Password : string) : boolean;
  2. var
  3.   FTP: TFTPSend;
  4.   FTPHost: string;
  5.   SourceFile: string;
  6.  
  7.    SR: TSearchRec;
  8.     Path : string;
  9. begin
  10.   FTP := TFTPSend.Create;
  11.   try
  12.     try
  13.       FTP.TargetHost := Host;
  14.       FTP.UserName := Username;
  15.       FTP.Password := Password;
  16.       FTP.Login; //ok
  17.       FTP.ChangeWorkingDir('Data'); //everything is ok
  18.     except
  19.       on E: Exception do
  20.       begin
  21.         ShowMessage('Exception: '+E.Message);
  22.         Exit;
  23.       end;
  24.     end;
  25.     with FTP do
  26.     begin
  27.               ShowMessage('Current FTP directory: '+FTP.TargetHost+FTP.GetCurrentDir);
  28.  
  29.               // ----------------------------
  30.       Path := FTP.GetCurrentDir;
  31.          frmUpload.Memo1.Clear;
  32.  
  33.       if FindFirst(Path+'*', faDirectory, SR) = 0 then
  34.         repeat
  35.     begin
  36.           if ((SR.Attr and faDirectory) = faDirectory) then
  37.  
  38.                 begin
  39.                frmUpload.Memo1.Append(Path+SR.Name);
  40.  
  41.       end
  42.       else
  43.       begin        
  44.       //  ShowMessage('Nada');
  45.       end;
  46.     end;
  47.  
  48.   until FindNext(SR) <> 0;
  49.  
  50.   FindClose(SR);
  51.         end;
  52.  
  53.     FTP.Logout;
  54.   finally
  55.    FTP.Free;
  56.   end;
  57.   ShowMessage('Done');
  58. end;        
  59.    

Code: Pascal  [Select]
  1. if  SendFTP('192.168.0.10', 'UserName', 'Pass') then ShowMessage('Connected');

On local is working well but no on remote.

Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #1 on: January 18, 2016, 10:13:13 pm »
If you don't mind using Indy, there is a quick and tested solution:

Code: Pascal  [Select]
  1.      FTP := TIdFTP.Create( nil );
  2.      FTP.Username := 'user123';
  3.      FTP.Password := 'pas123';
  4.      FTP.Host := 'xxx.xxx.xxx.xxx';
  5.      try
  6.         aLog('Intentando conectar para recuperar lista de archivos...');
  7.         FTP.Connect;
  8.         aLog('Conexión establecida.');
  9.      except
  10.  
  11.            aLog(DateTimeToStr(Now) + '  No se logro establecer la conexión con el servidor FTP.');
  12.            FTP.Free;
  13.            Terminate;
  14.  
  15.      end;
  16.  
  17.      FTP.ChangeDir( '/myfiles/' );
  18.      FTP.List('*.ZIP',true);
  19.  
  20.      numFiles := FTP.DirectoryListing.Count;
  21.  
  22.      SetLength(aFilesftp, numFiles);
  23.  
  24.      for I:= 0 to numFiles - 1 do
  25.          aFilesftp := FTP.DirectoryListing.Items.FileName;
  26.  
  27.      FTP.Disconnect;  
  28.  
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

christensen

  • Full Member
  • ***
  • Posts: 126
Re: FTP directory list
« Reply #2 on: January 19, 2016, 08:52:43 pm »
Thank you,unfortunately for me Indy doesn't work :(

Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

GetMem

  • Hero Member
  • *****
  • Posts: 3501
Re: FTP directory list
« Reply #3 on: January 19, 2016, 09:07:35 pm »
Hi christensen,

Quote
Thank you,unfortunately for me Indy doesn't work :(

You don't like it or you cannot install it?


Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #4 on: January 20, 2016, 03:49:58 pm »
You don't like it or you cannot install it?
Uh? Have you actually tried. I am using the latest Indy 10 from their SVN repository. I simply had to to correct the paths of about 8 units so that the indylaz.lpk package can find them - a 1 minute job maximum. Then I clicked Compile and Install and it worked perfectly.

See attached screenshot. For your convenience I also included the corrected indylaz.lpk package file. I'll submit these changes to the Indy project now as well, so it will be correct for everybody later today.

Indy's SVN repository works perfectly with FPC 2.6.4 and FPC 3.0 - tested under FreeBSD, Linux and Windows.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #5 on: January 20, 2016, 03:53:21 pm »
Thank you,unfortunately for me Indy doesn't work :(
What exactly doesn't work? I normally use Indy without a Lazarus Package - simply set up the source path, and use the components in code (like I do with any other classes).  As I just mentioned to GetMem, the only problem I found was that the 8 units in the indylaz.lpk package had outdated path locations. It took me a minute to fix them. After that the indylaz.lpk package compiled and install into Lazarus IDE without problems.

Please use Indy 10 from their SVN repository. You don't need to use any other "custom FPC" Indy archives any more.
 https://svn.atozed.com:444/svn/Indy10/trunk
« Last Edit: January 20, 2016, 03:58:07 pm by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

christensen

  • Full Member
  • ***
  • Posts: 126
Re: FTP directory list
« Reply #6 on: January 22, 2016, 09:38:28 pm »
I've install the SVN version but still i got proplems with listing the directory.

Code: Pascal  [Select]
  1. procedure TfrmDownload.Button1Click(Sender: TObject);
  2.  
  3. var
  4. numfiles, I: integer;
  5. aFilesftp: string;
  6.  
  7. begin
  8.    FTP := TIdFTP.Create( nil );
  9.      FTP.Username := 'UserName';
  10.      FTP.Password := 'Pass';
  11.      FTP.Host := '192.168.0.10';
  12.  
  13.      try
  14.         FTP.Connect;
  15.      
  16.         ShowMessage('Connected');
  17.        
  18.      except
  19.  
  20.            FTP.Free;
  21.                    ShowMessage('Disconnected');
  22.      end;
  23.  
  24.      FTP.ChangeDir( 'A' );
  25.      ShowMessage(ftp.RetrieveCurrentDir);
  26.  
  27.        FTP.List('*.*',true);
  28.  
  29.  
  30.        Memo1.Append(ftp.DirectoryListing.DirectoryName);
  31.  
  32.          {
  33.      numFiles := ftp.DirectoryListing.Count;
  34.  
  35.      SetLength(aFilesftp, numFiles);
  36.  
  37.      for I:= 0 to numFiles - 1 do
  38.          aFilesftp := ftp.DirectoryListing.GetNamePath;
  39.         Memo1.Append(ftp.DirectoryListing.DirectoryName);
  40.         }
  41.  
  42.      FTP.Disconnect;
  43.  
  44. end;      
  45.  

it shows the correct path on:  ShowMessage(ftp.RetrieveCurrentDir); but can't list the content :(

i got a permission error message.
Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

GetMem

  • Hero Member
  • *****
  • Posts: 3501
Re: FTP directory list
« Reply #7 on: January 22, 2016, 10:54:39 pm »
You don't like it or you cannot install it?
Uh? Have you actually tried. I am using the latest Indy 10 from their SVN repository. I simply had to to correct the paths of about 8 units so that the indylaz.lpk package can find them - a 1 minute job maximum. Then I clicked Compile and Install and it worked perfectly.

See attached screenshot. For your convenience I also included the corrected indylaz.lpk package file. I'll submit these changes to the Indy project now as well, so it will be correct for everybody later today.

Indy's SVN repository works perfectly with FPC 2.6.4 and FPC 3.0 - tested under FreeBSD, Linux and Windows.
I was only asking the OP if he/she can install Indy. Personally I'm a long time indy user, 100% satisfied.


Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #8 on: January 23, 2016, 01:09:44 am »
I've install the SVN version but still i got proplems with listing the directory.

Here is a tested and working example.

Code: Pascal  [Select]
  1. uses
  2.   // ftp client component
  3.   IdFTP,
  4.   // to support file enums
  5.   IdFTPList,
  6.   // to parse various file listings
  7.   IdFTPListParseUnix, IdFTPListParseWindowsNT { or use IdAllFTPListParsers for many others }
  8.   ;
  9.  
  10. procedure TMainForm.Log(const AMsg: string);
  11. begin
  12.   Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd HH:nn:ss', Now) + ' ' + AMsg);
  13. end;
  14.  
  15. procedure TMainForm.Button1Click(Sender: TObject);
  16. var
  17.   i: integer;
  18.   FTP: TIdFTP;
  19. begin
  20.   FTP := TIdFTP.Create(nil);
  21.   try
  22.     FTP.Username := 'anonymous';
  23.     FTP.Password := 'somebody@domain.com';
  24.     FTP.Host := 'ftp.freepascal.org';
  25.  
  26.     try
  27.       FTP.Connect;
  28.       Log('Connected');
  29.     except
  30.       FTP.Free;
  31.       Log('Disconnected');
  32.       Exit;
  33.     end;
  34.  
  35.     Log(ftp.RetrieveCurrentDir);
  36.     FTP.ChangeDir( '/pub/fpc/dist' );
  37.     Log(ftp.RetrieveCurrentDir);
  38.  
  39.     FTP.List('*', true);
  40.  
  41.     for i := 0 to FTP.DirectoryListing.Count-1 do
  42.     begin
  43.       if FTP.DirectoryListing[I].ItemType = ditFile then
  44.         Log(FTP.DirectoryListing[I].FileName)
  45.       else if FTP.DirectoryListing[I].ItemType = ditDirectory then
  46.         Log('[' + FTP.DirectoryListing[I].FileName + ']');
  47.     end;
  48.  
  49.   finally
  50.     FTP.Disconnect;
  51.     FTP.Free;
  52.   end;
  53. end;  
  54.  

Attached is a screenshot of the output.

For a detailed explanation of the FTP Client, and what might look like odd behaviour for directory listings. The following URL explains why Indy FTP Client is implemented as it is. Very informative.
   http://stackoverflow.com/questions/13689155/how-to-list-all-files-in-a-ftp-server
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

christensen

  • Full Member
  • ***
  • Posts: 126
Re: FTP directory list
« Reply #9 on: January 24, 2016, 09:13:29 pm »
Thanks, it work only with:

FTP.List('*', false); 

Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #10 on: January 25, 2016, 11:31:06 am »
Thanks, it work only with:
FTP.List('*', false);

I chose the "*" file mask because I was connecting to a FTP server running on a FreeBSD (unix) system. Unix systems are very common place on the internet. It is also common place on unix systems to have files without file extensions. So the "*" works for files with and without file extensions.

The "*.*" is primarily used on Windows systems and that file mask dictates that files must have a file extension. On a side note: Personally, I have never seen a Windows FTP server in the wild.
« Last Edit: January 25, 2016, 11:33:53 am by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 8939
Re: FTP directory list
« Reply #11 on: January 25, 2016, 12:10:20 pm »
On a side note: Personally, I have never seen a Windows FTP server in the wild.

Well, IIS supports ftp  (after a little configuration) and always has, so unless you are not connected to the Internet, which you obviously are....
https://technet.microsoft.com/nl-nl/library/cc732769%28v=ws.10%29.aspx

But this goes for much older versions as well. It even used to be enabled by default until ftp got a bad name, that is..

I would say you never noticed instead of never seen ;)
« Last Edit: January 25, 2016, 12:20:50 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #12 on: January 25, 2016, 03:03:37 pm »
Well, IIS supports ftp  (after a little configuration) and always has,
I didn't mean it doesn't exist on Windows... I just meant it is hardly ever seen. Just about every publicly accessible FTP server on the Internet is hosted on some or other unix-like system.

Quote
... until ftp got a bad name, that is..
If you are talking about security, FTPS has existed for years. Internal LAN based FTP is perfect too, and faster because of no security overhead. If you want to transfer files very fast, FTP is the protocol you want to use. SAMBA, NFS, AirDrop etc are all a LOT slower than FTP. If you want a mixed platform of systems to transfer files easily (eg: OSX to FreeBSD or Haiku to OSX or Windows to Linux etc), FTP is your answer, and the easiest solution by far. Plus you get some fantastic FTP Client software with amazing features (FileZilla comes to mind).
« Last Edit: January 25, 2016, 03:19:59 pm by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 8939
Re: FTP directory list
« Reply #13 on: January 25, 2016, 04:38:48 pm »
Did I mention ftps or sftp? Thought not. ftp got a bad name. The two secure ones are ok.
Most people that want to use threading should learn to patch their jeans first: use a needle.

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: FTP directory list
« Reply #14 on: January 25, 2016, 05:22:25 pm »
Did I mention ftps or sftp?
As we were talking about the FTP protocol, I mentioned FTPS, as that is the FTP protocol with SSL support.

The SFTP is actually SSH File Transfer Protocol, which is a completely different protocol to FTP. Yes the names are rather confusing. ;-)

Quote
ftp got a bad name.
FTP got invented some 40 years ago - long before security was any concern. So no, you wouldn't want to use FTP over the internet, but using FTP behind a firewall in a LAN environment I think is okay (if you trust your employees). eg: I use FTP in my home office environment so moving files between our FreeBSD file server and our FreeBSD, Linux and OSX workstations is a breeze (and very fast).

Anyway, I think we agree in general - and straying a bit off topic. ;)
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/