Cry about...
Delphi Programming with WinInet
FtpCommand Example
Using FtpCommand to list folder contents
The following code fragment shows how to use FtpCommand
to list the contents of a folder. (To list the contents of a folder
use FtpFindFirstFile
and InternetFindNextFile
- this code fragment is for illustration purposes only.)
function FtpCommandDirList(hFtpSession: HINTERNET): String; var data_handle: HINTERNET; bytes_read: DWORD; text_buffer: PChar; buffer_size: DWORD; begin result := ''; data_handle := nil; if not FtpCommand(m_hFtpSession,true,FTP_TRANSFER_TYPE_ASCII, PChar('NLST'),0,@data_handle) then begin SetErrorCode(GetLastError()); Application.MessageBox(PChar(SysErrorMessage(GetLastError())),''); exit; end; // Read the data. buffer_size := 1000; // Arbitrary value. GetMem(text_buffer,buffer_size); bytes_read := 1; while bytes_read > 0 do begin InternetReadFile(data_handle,text_buffer,buffer_size, bytes_read); result := result + System.Copy(text_buffer,0,bytes_read); end; InternetCloseHandle(data_handle); FreeMem(text_buffer,buffer_size); end;
Note:
- If (in this example) FtpCommand fails then the most likely cause is that the directory is empty.
- If you get an error from Delphi complaining that the number
of arguments to FtpCommand is wrong then the definition for
FtpCommand in WinInet is probably missing the last argument.
The correct definition is provided at the bottom of my
write-up on the
FtpCommand
.
These notes are believed to be correct for Delphi 6 and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.