uses
WinInet;
function FtpGetFiles(hInet: HInternet; user, password, server, localDir, remoteDir:
String): Boolean;
var
fd: TWin32FindData;
hSession, hSearch: HINTERNET;
begin
Result := False;
hSession := InternetConnect(hInet,
PChar(server), 21, PChar(user), PChar(password),
INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0
);
if Assigned(hSession)
then
begin
if not FtpSetCurrentDirectory(hSession, PChar(remoteDir))
then
Exit;
localDir := IncludeTrailingPathDelimiter(localDir);
if not DirectoryExists(localDir)
then
MkDir(localDir);
ChDir(localDir);
hSearch := FtpFindFirstFile(hSession,
nil, fd, 0, 0);
if Assigned(hSearch)
then
begin
Result := True;
repeat
if (fd.dwFileAttributes
and FILE_ATTRIBUTE_DIRECTORY = 0)
then
Result := FtpGetFile(hSession,
fd.cFileName, PChar(localDir + fd.cFileName),
False, 64, FTP_TRANSFER_TYPE_UNKNOWN, 0
)
else
if Pos(fd.cFileName, '
..') = 0
then
Result := FtpGetFiles(hInet,
user, password, server, localDir + fd.cFileName,
remoteDir + fd.cFileName
);
until not (Result
and InternetFindNextFile(hSearch, @fd));
InternetCloseHandle(hSearch);
end;
InternetCloseHandle(hSession);
end;
end;
procedure Test;
var
hInet: HINTERNET;
begin
hInet := InternetOpen('
Test', INTERNET_OPEN_TYPE_DIRECT,
nil,
nil, 0);
if Assigned(hInet)
then
try
FTPGetFiles(hInet, '
herbert', '
toppsigrid', '
localhost', '
c:\temp\hsbc\', '
/hsbc/');
finally
InternetCloseHandle(hInet);
end;
end;