Evo mog .pas fajla:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Psock, NMFtp;
//define ftp item type
type
TFTPItem = record
Name,User,Group,Permissions,Date:string;
Size,Number:integer;
IsFile:boolean;
end;
type
TForm1 = class(TForm)
NMFTP1: TNMFTP;
procedure NMFTP1ListItem(Listing: String);
procedure FormCreate(Sender: TObject);
procedure NMFTP1Connect(Sender: TObject);
private
function ParseFTPItem(pFTPItem: string): TFTPItem;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.ParseFTPItem(pFTPItem:string):TFTPItem;
var tmp,zFTPItem,date:string;
i,o:integer;
begin
zFTPItem:=pFTPItem;
//parse given string
zFTPItem:=Trim(zFTPItem); //remove blanks
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
Result.Permissions:=trim(tmp); //whole tmp is permissions, but first char tells us whether this is file or not
if tmp[1]='d' then Result.IsFile:=False //find out whether item is file or not
else Result.IsFile:=True;
//let's extract stupid number :)
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
Result.number:=StrToInt(tmp[1]); //we got number!!!
//now user is our target
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
Result.User:=trim(tmp); //this should be user
//group is next
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
Result.Group:=trim(tmp); //this should be group
//let's now get file size
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
Result.Size:=strtoint(trim(tmp)); //this should be size
//date...
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
date:=Trim(tmp);
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
date:=date+'-'+Trim(tmp);
i:=POS(' ',zFTPItem); //find space (end of word)
tmp:=copy(zFTPItem,0,i); //copy till space (1st word)
zFTPItem:=StringReplace(zFTPItem,tmp,'',[]); //remove word from FTPItem
zFTPItem:=Trim(zFTPItem); //remove blanks
date:=date+'-'+Trim(tmp);
Result.Date:=trim(date); //this should be date
//finally, get item's name
Result.Name:=trim(zFTPItem); //this should be name
end;
procedure TForm1.NMFTP1ListItem(Listing: String);
var item:tftpitem;
begin
item:=ParseFTPItem(Listing);
ShowMessage('Name: '+item.Name);
ShowMessage('Size: '+IntToStr(item.Size));
ShowMessage('Is File? '+BoolToStr(item.IsFile,true));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NMFTP1.Host:='localhost';
NMFTP1.UserID:='avram';
NMFTP1.Password:='moj_pass';
NMFTP1.Connect;
end;
procedure TForm1.NMFTP1Connect(Sender: TObject);
begin
NMFTP1.ChangeDir('www');
NMFTP1.List;
end;
end.