Downloading all files (*.*) from within Delphi
Date Saturday, December 08 @ 15:48:16
Topic Internet & Web


You can use this code to download all files from an FTP site from within your Delphi program.

Uses WININET;

function TMyFtp.FindFiles: TStringList;
var
FindData: TWin32FindData;
FindHandle: HInternet;
Begin
FindHandle := FtpFindFirstFile(FFtphandle, '*.*',
FindData, 0, 0);
If FindHandle = nil Then
Begin
Result := nil;
Exit;
End;
FCurFiles.Clear;
FCurFiles.Add(GetFindDataStr(FindData));

While InternetFindnextFile(FindHandle, @FindData) Do
FCurFiles.Add(GetFindDataStr(FindData));
InternetCloseHandle(Findhandle);
GetCurrentDirectory;
Result := FCurFiles;
End;

function GetFindDataStr(FindData: TWin32FindData): string;

//Get current files in directory

var
S: string;
Temp: string;
Begin
S := S + GetDots(75);
Move(FindData.CFilename[0], S[6], StrLen(FindData.CFileName));
Temp := IntToStr(FindData.nFileSizeLow);
Move(Temp[1], S[25], Length(Temp));
Result := S;
End;

function TMyFtp.FindFiles: TStringList;
var
FindData: TWin32FindData;
FindHandle: HInternet;
Begin
FindHandle := FtpFindFirstFile(FFtphandle, '*.pjl',
FindData, 0, 0);

If FindHandle = nil then
Begin
Result := nil;
Exit;
end;

FCurFiles.Clear;
FCurFiles.Add(GetFindDataStr(FindData));

While InternetFindnextFile(FindHandle, @FindData) Do

FCurFiles.Add(GetFindDataStr(FindData));
InternetCloseHandle(Findhandle);
GetCurrentDirectory;
Result := FCurFiles;

End;

function TECU.GetFile(FTPFile, NewFile: string): Boolean;

//Download a File Through FTP

begin
Result := FtpGetFile(FFTPHandle, PChar(FTPFile), PChar(NewFile), False, File_Attribute_Normal, Ftp_Transfer_Type_Binary, 0);
end;

Procedure Button1.Onclick();
ar
MyHandle: HINTERNET; //handle from InternetConnect
Begin
MyHandle := InternetOpen(`ECU', 0, nil, 0, 0);
InternetConnect(MyHandle, ftp://ftp.mysite.com, 0, Anonymouse, user@mysite.com, INTERNET_SERVICE_FTP, INTERNET_CONNECT_FLAG_PASSIVE, 0 );

//Changes to a specific directory once connected
TECU.ChangeDirExact('ftp://ftp.mysite.com/downloads');
FindHandle := FtpFindFirstFile(FFtphandle, '*35L.PJL', FindData, 0, 0);

// If the directory is blank, then tell me about it. Else, download all.
If FindHandle = nil Then
Begin
Result := nil;
ShowMessage('The file was not able to be downloaded. Please Re-Try');
End

Else
TECU.GetFile('*.*', 'c:\*.*');
End;
End;


//The call InternetConnect Uses the following function ( included with WinInet ):

{*******************************************************************************
function InternetConnect(
hInet: HINTERNET; // Handle from InternetOpen
lpszServerName: PChar; // Server: i.e., www.borland.com
nServerPort: INTERNET_PORT; // Usually 0
lpszUsername: PChar; // usually anonymous
lpszPassword: PChar; // usually your email address
dwService: DWORD; // FTP, HTTP, or Gopher?
dwFlags: DWORD; // Usually 0
dwContext: DWORD): // User defined number for callback
HINTERNET; stdcall;

*******************************************************************************}




This article comes from Delphi Swag
http:/www.dswag.com

The URL for this story is:
http:/www.dswag.com/article.php?sid=9