这是我工作需要而作的,她会检测两台计算机指定目录的文件的最后修改日期,判断后自动
更新,如果一台机器上没有文件,而另一台有的话会自动拷贝过来..
////////////////////////////////
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,WinINet,Inifiles,shellapi, Psock, NMFtp,
ExtCtrls;
type
Taskar = class(TForm)
updata: TButton;
exit: TButton;
search: TButton;
Label1: TLabel;
Memo1: TMemo;
Label2: TLabel;
Memo2: TMemo;
pb1: TProgressBar;
sb1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure searchClick(Sender: TObject);
procedure exitClick(Sender: TObject);
procedure updataClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
askar: Taskar;
hinet,ftphandle,hfind:HINTERNET;
stime,kong:string;
//////////////////
lpfindfiledata:TWin32FindData;
myini:TInifile;
searchrec:TSearchRec;
Rval,iport,flag:integer;
mybool:LONGBOOL;
ftpsname,localsname,lsdatetime,rsdatetime:string;
strip,strname,strpass,strlpath,strrpath:string;
//////////////////
implementation
{$R *.dfm}
function GetlfileDate(FileName:String):string; ///得到本地文件日期
var
h: THandle;
Struct: TOFSTRUCT;
lastwrite: integer;
begin
h:=OpenFile(PChar(FileName),Struct,OF_SHARE_DENY_NONE);
try
if h<>HFILE_ERROR then
begin
lastwrite:=FileGetDate(h);
Result:=datetimetostr(FileDateToDateTime(lastwrite));
end;
finally
_lclose(h);
end;
end;
//////////////
function ftpgetfiletime(ftime:TFILETIME):string;
var
localfiletime:TFILETIME;
dosfiletime
WORD;
the_time:TDateTime;
begin
FileTimeToLocalFileTime(ftime, LocalFileTime);
FileTimeTodosDateTime(ftime,LongRec(DosFileTime).Hi,LongRec(DosFileTime).Lo);
The_time:=FileDateToDateTime(DosFileTime);
Result:=DateTimeToStr(The_Time);
end;
////获得FTP文件最后修改日期
procedure Taskar.FormCreate(Sender: TObject);
begin
kong:=' ';
myini:=TInifile.Create(extractfilepath(paramstr(0))+'format.ini');
strip:=myini.ReadString('hostid','id','');
strname:=myini.ReadString('user','name','');
strpass:=myini.ReadString('user','pass','');
strlpath:=myini.ReadString('dir','localpath','')+'/';
strrpath:=myini.ReadString('dir','remotepath','');
iport:=strtoint(myini.ReadString('port','dport',''));
if assigned(ftphandle) then internetclosehandle(ftphandle);
hinet:=internetopen(pchar('askar'),0,nil,nil,0);
if assigned(hinet) then
begin
ftphandle:=internetconnect(hinet,PChar(strip),iport,PChar(strname),PChar(strpass),
internet_service_ftp,internet_flag_passive,0);
if assigned(ftphandle) then begin
sb1.SimpleText:='连接成功';
ftpsetcurrentdirectory(ftphandle,PChar(strrpath));
end
else begin
application.MessageBox('连接失败!请检查网络连接是否正常, FTP 服务是否正常或用户名,密码是否对。。','错误信息',mb_ok);
application.Terminate;
end;
end;
/////////////////连接部分以完成
//////////比较日期开始:
////////////local file///////////////////
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,0,0);
repeat
if string(searchrec.Name)=string(lpfindfiledata.cFileName) then
begin
if getlfiledate(strlpath+searchrec.Name)>ftpgetfiletime(lpfindfiledata.ftLastWriteTime)
then begin
memo2.Lines.Add(searchrec.Name+kong+getlfiledate(strlpath+searchrec.Name));
break;
end else break;
end;
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
until mybool=false;
if (searchrec.Name='.')or(searchrec.Name='..')then flag:=0 else begin
if string(searchrec.Name)<>string(lpfindfiledata.cFileName)
then memo2.Lines.Add(searchrec.Name+kong+getlfiledate(strlpath+searchrec.Name));
end;
rval:=findnext(searchrec);
until rval<>0;
///////////////// ftp sever//////////////////
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,0,0);
repeat
if not assigned(hfind) then break;
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
if string(lpfindfiledata.cFileName)=string(searchrec.Name) then
begin
if ftpgetfiletime(lpfindfiledata.ftLastWriteTime)>getlfiledate(strlpath+searchrec.Name)
then begin
memo1.Lines.Add(lpfindfiledata.cFileName+kong+ftpgetfiletime(lpfindfiledata.ftLastWriteTime));
break;
end else break;
end;
rval:=findnext(searchrec);
until rval<>0;
if (string(searchrec.Name)<>string(lpfindfiledata.cFileName))then memo1.Lines.Add(lpfindfiledata.cFileName+kong+ftpgetfiletime(lpfindfiledata.ftLastWriteTime));
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
until mybool=false;
if (memo1.Text='')and (memo2.Text='') then begin updata.Enabled:=false end;
end;
///////////////////
///////////////////
///////////////////
procedure Taskar.searchClick(Sender: TObject);
begin
memo1.Clear;
memo2.Clear;
///////////////
if assigned(ftphandle) then internetclosehandle(ftphandle);
hinet:=internetopen(pchar('askar'),0,nil,nil,0);
if assigned(hinet) then
begin
ftphandle:=internetconnect(hinet,PChar(strip),iport,PChar(strname),PChar(strpass),
internet_service_ftp,internet_flag_passive,0);
if assigned(ftphandle) then begin
sb1.SimpleText:='连接成功';
ftpsetcurrentdirectory(ftphandle,PChar(strrpath));
end
else begin
application.MessageBox('连接失败!请检查网络连接是否正常, FTP 服务是否正常或用户名,密码是否对。。','错误信息',mb_ok);
application.Terminate;
end;
end;
/////////////////连接部分以完成
/////////////////
/////////////ftpserver///////////
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,INTERNET_FLAG_RESYNCHRONIZE,0);
repeat
if not assigned(hfind) then break;
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
if string(lpfindfiledata.cFileName)=string(searchrec.Name) then
begin
if ftpgetfiletime(lpfindfiledata.ftLastWriteTime)>getlfiledate(strlpath+searchrec.Name)
then begin
memo1.Lines.Add(lpfindfiledata.cFileName+kong+ftpgetfiletime(lpfindfiledata.ftLastWriteTime));
break;
end else break;
end;
rval:=findnext(searchrec);
until rval<>0;
if (string(searchrec.Name)<>string(lpfindfiledata.cFileName))then memo1.Lines.Add(lpfindfiledata.cFileName+kong+ftpgetfiletime(lpfindfiledata.ftLastWriteTime));
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
until mybool=false;
//////////////////////////////////
////////////////localfile////////////
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,0,0);
repeat
if string(searchrec.Name)=string(lpfindfiledata.cFileName) then
begin
if getlfiledate(strlpath+searchrec.Name)>ftpgetfiletime(lpfindfiledata.ftLastWriteTime)
then begin
memo2.Lines.Add(searchrec.Name+kong+getlfiledate(strlpath+searchrec.Name));
break;
end else break;
end;
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
until mybool=false;
if (searchrec.Name='.')or(searchrec.Name='..')then flag:=0 else begin
if string(searchrec.Name)<>string(lpfindfiledata.cFileName)
then memo2.Lines.Add(searchrec.Name+kong+getlfiledate(strlpath+searchrec.Name));
end;
rval:=findnext(searchrec);
until rval<>0;
////////////////localfile////////////
if not ((memo1.Text='')and (memo2.Text='')) then begin updata.Enabled:=true end;
if (memo1.Text='')and (memo2.Text='') then begin updata.Enabled:=false end;
end;
///////////////////////
//////////////////////
procedure Taskar.exitClick(Sender: TObject);
begin
internetclosehandle(hinet);
internetclosehandle(ftphandle);
internetclosehandle(hfind);
application.Terminate;
end;
////////////////////
////////////////////
/////////////////////
procedure Taskar.updataClick(Sender: TObject);
var
ps,i,msum:integer;
begin
ps:=0;
i:=0;
msum:=1;
search.Enabled:=false;
updata.Enabled:=false;
exit.Enabled:=false;
try
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,0,0);
repeat
if string(searchrec.Name)=string(lpfindfiledata.cFileName) then
begin
if getlfiledate(strlpath+searchrec.Name)>ftpgetfiletime(lpfindfiledata.ftLastWriteTime)
then begin
ftpputfile(ftphandle,pchar(strlpath+searchrec.Name),pchar(strrpath+searchrec.Name),
ftp_transfer_type_binary,0);
break;
end else break;
end;
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
pb1.Position:=pb1.Position+msum;
until mybool=false;
if (searchrec.Name='.')or(searchrec.Name='..')
then flag:=0 else begin
if string(searchrec.Name)<>string(lpfindfiledata.cFileName)
then ftpputfile(ftphandle,pchar(strlpath+searchrec.Name),pchar(strrpath+searchrec.Name),
ftp_transfer_type_binary,0);
end;
rval:=findnext(searchrec);
pb1.Position:=pb1.Position+msum;
until rval<>0;
////////////////// ftp sever//////////////////
hfind:=ftpfindfirstfile(ftphandle,nil,lpfindfiledata,0,0);
repeat
rval:=findfirst(strlpath+'*.*',faanyfile,searchrec);
repeat
if string(lpfindfiledata.cFileName)=string(searchrec.Name) then
begin
if ftpgetfiletime(lpfindfiledata.ftLastWriteTime)>getlfiledate(strlpath+searchrec.Name)
then begin FtpGetFile(FtpHandle, PChar(strrpath+ lpfindfiledata.cFileName), PChar(strlpath + lpfindfiledata.cFileName),
false,0, FTP_TRANSFER_TYPE_BINARY,0);
break;
end else break;
end;
rval:=findnext(searchrec);
pb1.Position:=pb1.Position+msum;
until rval<>0;
if (string(searchrec.Name)<>string(lpfindfiledata.cFileName))
then FtpGetFile(FtpHandle, PChar(strrpath+ lpfindfiledata.cFileName), PChar(strlpath+lpfindfiledata.cFileName),
false,0, FTP_TRANSFER_TYPE_BINARY,0);
mybool:=internetfindnextfile(hfind,@lpfindfiledata);
pb1.Position:=pb1.Position+msum;
until mybool=false;
pb1.Position:=pb1.Position+(100-pb1.Position);
search.Enabled:=true;
updata.Enabled:=true;
exit.Enabled:=true;
ps:=10000000;
for i:=1 to ps do
begin
if i=10000000 then pb1.Position:=0;
end;
except
application.MessageBox('传送数据是发生错误。。。。','错误',mb_ok);
Application.Terminate;
end;
internetclosehandle(hinet);
internetclosehandle(ftphandle);
internetclosehandle(hfind);
end;
procedure Taskar.FormClose(Sender: TObject; var Action: TCloseAction);
begin
internetclosehandle(hinet);
internetclosehandle(ftphandle);
internetclosehandle(hfind);
end;
end.