Title: Download *.* From an FTP in Code
Question: How can I download all files using *.* or other wildcard characters from within Delphi?
Answer:
You will have to try this out, as I'm at work and can't really do it, but, heh, again, I'm at work. So, I got bored, and wrote this up real quick. If you all find any problems, or have any solutions, please don't hesitate to post.... Anyyyways, without further procrastination, here ya go:
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;
*******************************************************************************}
// Again, this is not 100% concrete to work, so, make it concrete by posting ;)
// Thanks