?»??»?unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, IdFTPCommon, IdFTPList, StrUtils, ExtCtrls,inifiles,shellapi; type TASUSBackup = class(TService) IdFTP1: TIdFTP; Timer1: TTimer; IdFTP2: TIdFTP; procedure Timer1Timer(Sender: TObject); procedure ServiceExecute(Sender: TService); private BytesToTransfer: LongWord; function GetHostInfo(header: String): String; procedure CheckFTP(IdFTP:Tidftp;hostser:string); procedure AppendDebug( S: string); procedure AppendLog( S: string); procedure testDebug(s:string); function CreatePath( const Path: string): boolean; function GetFileData( FileName: string): string; procedure GetFiles(idftp:Tidftp;Host:string); function RightPos(Substr, S:string):Integer; public function GetServiceController: TServiceController; override; end; const Debug = True; var ASUSBackup: TASUSBackup; PreDir:string; implementation {$R *.DFM} function TASUSBackup.GetHostInfo(header: String): String; var ServerName: String; ServerIni: TIniFile; begin ServerIni := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'ini\FtpHost.ini'); ServerName := ServerIni.ReadString('Server', header, header); ServerIni.Free; result := ServerName; end; //³s½u¦ÜFTP Server ¨Ã¨ú±o¤å¥ó procedure TASUSBackup.CheckFTP(IdFTP:Tidftp;hostser:string); var UserID1, Password1, LogonPath1,Host1: string; dest:TStringlist; i:integer; begin if IdFTP.Connected then begin try IdFTP.Quit; except end; end; try UserID1 := GetHostInfo('USERID'+hostser); Password1 := GetHostInfo('PWD'+hostser); LogonPath1 := GetHostInfo('LOGONPATH'+hostser); Host1:= GetHostInfo('FTPHOST'+hostser); IdFTP.Username := UserID1; IdFTP.Password := Password1; IdFTP.Host := Host1; testdebug('we are on CheckFTP!'#13#10); try IdFTP.Connect( True, 5000); AppendDebug( '[FTPHost' + hostser +' ]'); try IdFTP.DirectoryListing.Clear; except testdebug('ftp directory clear Error!'#13#10); end; idftp.TransferType:=ftascii;//³o¼Ë³]¸m¬O¦]¬°¤U­±¥Î¨ì¤Fidftp.list¡A¥²¶·³o¼Ë³] try testdebug('LogonPath1: '+LogonPath1+#13#10); IdFTP.ChangeDir(LogonPath1); sleep(500); try testdebug('FtpList...'#13#10); dest:=TStringlist.Create; idftp.List(dest,'*.*',true); if dest.Count >0 then begin testdebug('ftplist:'#13#10); for i:=0 to dest.Count-1 do begin testdebug(#9+dest.Strings[i]+#13#10); end; end; dest.Free; //IdFtp.List(nil, '*.*', True); testdebug('FtpListOK!'#13#10); sleep(100); //testdebug('FtpTransferType!'#13#10); idftp.TransferType:=ftbinary; testdebug('before GetFiles!'#13#10); GetFiles(idftp,host1); testdebug('after GetFiles!'#13#10); IdFTP.Disconnect; except testdebug('ErrorOccur in checkFTP!'#13#10); end;//list except testdebug('ChangeDir Error!'#13#10); end; //change dir except testdebug('ftp connect Error!'#13#10); end;//connect finally if IdFTP.Connected then IdFTP.Quit; end; //finally end; //±qFTP Server¤WDownload Files procedure TASUSBackup.GetFiles(idftp:Tidftp;Host:string); var i, j, h, ServerDate, l: integer; Name, Path, FileName, Buf, Buf2: string; f: TextFile; ff:file of byte; begin testdebug('start GetFiles!'+inttostr(idftp.DirectoryListing.Count)+#13#10); if idftp.DirectoryListing.Count =0 then exit; testdebug('begin!'#13#10); for i := 0 to IdFTP.DirectoryListing.Count - 1 do begin if IdFTP.DirectoryListing.Items[i].ItemType = ditFile then begin Name := IdFTP.DirectoryListing.Items[i].FileName; if UpperCase(RightStr(Name, 5)) = '.ASUS' then //¹ï¦W¦r«áºó¬°.asusuªº¾Þ§@ begin IdFTP.TransferType:= ftBinary; BytesToTransfer := IdFTP.Size(Name); try //downloag¤U¨Ó IdFTP.Get(Name, Name, true); //¨ìsystem32¤U¤F,¤£¬O¨Ï¥ÎÄò¶Ç AssignFile( f, Name); Reset( f); try //Ū¨ú¥X¨Ó Readln( f, FileName); Readln( f, Path); CloseFile( f); if (FileName > '') and (Path > '') then begin //¤§«ePath«e¦³§PÂ_­Y«e¤T­Ó¦r²Å¬°K:\ k:\ J:\ j:\,¦ý¬O·|­­¨î¤F¥H«á¨Ï¥Î Path := PreDir + copy( Path, 4, Length( Path) - 3); //PreDir¬O·í«eµ{§Çªº¤W¼h¸ô®|¡A§t\²Å¸¹¡A¬°¨ú±o¥¿½Tªº¸ô®|¡AÀ³·í¨Ï¸Óµ{§Ç¥Ø¿ý»PASUS_PART¦P¯Å //³o¥D­n¬O¦]¬°¨Ï¥Îºôµ¸¦@¨É«þ¨©¤ÎºÏ½L¬M®g«þ¨©¥¢±Ñ //ausu¥ÎL,pegatron¥ÎK AppendLog( 'DownLoad File From Host: '+Host+' ' +rightstr(path,strlen(pchar(path))-strlen(pchar(PreDir)))+ '\' + FileName + ' ...'); ServerDate := 0; for j := 0 to IdFTP.DirectoryListing.Count - 1 do begin if (IdFTP.DirectoryListing.Items[j].ItemType = ditFile) and (UpperCase(IdFTP.DirectoryListing.Items[j].FileName) = UpperCase(FileName)) then begin ServerDate := DateTimeToFileDate( IdFTP.DirectoryListing[j].ModifiedDate); break; end; end; IdFTP.TransferType:= ftBinary; BytesToTransfer := IdFTP.Size(FileName); sleep(500); IdFtp.Get(FileName,FileName, true);//¨ìsystem32¤U ¡A¤£¨Ï¥ÎÄò¶Ç h := FileOpen( FileName, fmShareDenyNone + fmOpenWrite); FileSetDate( h, ServerDate); FileClose( h); //³]¸mserverDate AssignFile( ff,FileName); Reset( ff); l := FileSize( ff); CloseFile( ff); //¨ú¥X¤å¥ó¤j¤pl //±NReadOnly Clear ¥Ø¼Ð¤å¥óÄݬ۲M°£ FileSetAttr(Path + '\' + FileName, 0); CreatePath( Path + '\' + FileName); //¹Á¸Õ³sÄò³Ð«Ø¥Ø¿ý if CopyFile(PChar(FileName + #0), PChar(Path+'\'+FileName+ #0), False) then //«þ¨©¤å¥ó¨ì¥Ø¼Ð¤å¥ó begin Buf := GetFileData( FileName); //·í«e¤å¥ó Buf2 := GetFileData( Path + '\'+FileName); //¥Ø¼Ð¤å¥ó if (Buf > '') and (Buf2 > '') and (Buf = Buf2) then //«þ¨©¦¨¥\´N§R°£FTP¹ïÀ³¨â­Ó¤å¥ó¤Î·í«e¥Ø¿ý¤å¥ó begin AppendLog( ' ' + IntToStr(l) + ' OK'#13#10); try IdFTP.Delete(Name); except end; try IdFTP.Delete( FileName); except end; DeleteFile( FileName); end else begin //«þ¨©¥¢±ÑLog²K¥[Error AppendLog( ' Error1'#13#10); AppendDebug( '[ErrorByBuffer]'); end; end else begin //«þ¨©¥¢±ÑLog²K¥[Error AppendLog( ' Error'#13#10); AppendDebug( '[CopyError ]'); end; end; DeleteFile( Name); //°õ¦æ§¹¦¨§R°£asus¤å¥ó except //Ū¨ú¨Ã¦b°õ¦æ¤¤¶¡¥¢±Ñ AppendDebug( '[ErrorFlag ]'); AppendLog( ' Error2'#13#10); CloseFile( f); DeleteFile( Name); //Ū¨ú¨Ã¦b°õ¦æ¤¤¶¡¥¢±Ñ¡A§R°£¥»¦a.asus¤å¥ó exit; end; except //download¹L¨Ó¥¢±Ñ AppendLog( ' Error3'#13#10); exit; end; end; //µ²§ô¹ï.asus¤å¥óªº¾Þ§@ sleep(100); end; //µ¥«Ý0.1¬í«á­«·s¨ú¤U¤@­Ó¤å¥ó¨Ã°õ¦æ§PÂ_¤Î¾Þ§@µ²§ô end; //for´`Àôµ²§ô testdebug('end!'#13#10); end; function TASUSBackup.CreatePath( const Path: string): boolean; var S: string; n: integer; begin try S := Path; n := PosEx( '\', Path, 4); while n > 0 do begin S := Copy( Path, 1, n); if not DirectoryExists( S) then mkdir( S); n := PosEx( '\', Path, n + 1); end; Result := True; except Result := False; end; end; function TASUSBackup.GetFileData( FileName: string): string; var f: file; l, NumRead: longint; S: string; begin try AssignFile( f, FileName); Reset( f, 1); l := FileSize( f); SetLength( S, l); BlockRead( f, S[1], l, NumRead); if NumRead = l then Result := S else Result := ''; CloseFile( f); except Result := ''; end; end; procedure TASUSBackup.AppendLog( S: string); var FileHandle : Integer; ss: string; begin try ss := ExtractFilePath(ParamStr(0)) + FormatDateTime( 'yyyymmdd', Now) + '.txt'; if not FileExists( ss) then begin FileHandle := FileCreate( ss); FileClose( FileHandle); end; FileHandle := FileOpen( ss, fmOpenWrite or fmShareDenyNone); if FileHandle > 0 then begin S := FormatDateTime( 'yyyymmdd hh:nn:ss ', Now) + S; FileSeek( FileHandle, 0, 2); FileWrite( FileHandle, S[1], Length(S)); FileClose( FileHandle); end; except end; end; procedure TASUSBackup.testDebug(s:string); var testHandle:integer; ss:string; begin ss:=ExtractFilePath(ParamStr(0))+ FormatDateTime( 'yyyymmdd', Now-1)+'testdebug.txt'; if fileexists(ss) then deletefile(ss);//¥u¥Îtestdebug.txt¡A¤å¥ó·|µL­­¼W¤j¡A ³o¼Ë¥u·|«O¯d·í¤Ñtestdebug.txt ss:=ExtractFilePath(ParamStr(0))+ FormatDateTime( 'yyyymmdd', Now)+'testdebug.txt'; if not Fileexists(ss) then begin testHandle:=FileCreate(ss); Fileclose(testHandle); end; testHandle:=FileOpen(ss,fmOpenWrite or fmShareDenyNone); if testHandle>0 then begin FileSeek(testHandle,0,2); FileWrite(testHandle,s[1],length(s)); FileClose(testHandle); end; end; //Output Information For Debug procedure TASUSBackup.AppendDebug( S: string); var FileHandle : Integer; ss: string; begin try if not Debug then exit; ss := ExtractFilePath(ParamStr(0)) + 'debug.txt'; if not FileExists( ss) then begin FileHandle := FileCreate( ss); FileClose( FileHandle); end; FileHandle := FileOpen( ss, fmOpenWrite or fmShareDenyNone); if FileHandle > 0 then begin S := FormatDateTime( 'yyyymmdd hh:nn:ss ', Now) + S; FileWrite( FileHandle, S[1], Length(S)); FileClose( FileHandle); end; except end; end; procedure ServiceController(CtrlCode: DWord); stdcall; begin ASUSBackup.Controller(CtrlCode); end; function TASUSBackup.GetServiceController: TServiceController; begin Result := ServiceController; end; procedure TASUSBackup.Timer1Timer(Sender: TObject); begin try //·í°õ¦æ§¹²¦¡A³o­Ó®É­Ô·|°±¤î¤@¤ÀÄÁµM«áÄ~Äò°õ¦æ¦¹¹Lµ{ Timer1.Enabled := False; PreDir:=leftstr(ExtractFileDir(ParamStr(0)),RightPos('\',ExtractFileDir(ParamStr(0)))-1); //¤£­n¨Ï¥ÎGetCurrentDir±o¨ìªº¬Osystem32 PreDir:=leftstr(PreDir,RightPos('\',PreDir));//¤W¨â¯Å¥Ø¿ý §t\ testDebug('PreDir:'+PreDir+#13#10); AppendDebug( '[00000 ]'); try testDebug('before CheckFTP idftp1!'#13#10); CheckFTP(idftp1,'1'); testDebug('after CheckFTP idftp1!'#13#10); except end; try if GetHostInfo('HOSTNUM')='3' then //¦pªGHOSTNUM¼g3ªí¥Ü¤T¤è¤è¦P¨B¡A¦¹®É°õ¦æ¨ä¥¦«H®§¡A©Ò¥H¦b³o¸Ì¼g¬O¦]¬°¤£¥Î­«±Òµ{¦¡¤]¥i¥H­×§ï begin try testDebug('before CheckFTP idftp2!'#13#10); CheckFTP(idftp2,'2'); testDebug('after CheckFTP idftp2!'#13#10); except end; end except // ¦pªGHOSTNUM¼gªº¤£¬O¼Æ¦r¤£·|³ø¿ù end finally AppendDebug( '[ExcDone ]'); Timer1.Interval :=60000; // 1 min Timer1.Enabled := True; end; end; function TASUSBackup.RightPos(Substr, S:string):Integer; begin Result:=Pos(ReverseString(Substr), ReverseString(S)); if Result >0 then Result:=Length(S) -Result+1- Length(Substr)+1; end; procedure TASUSBackup.ServiceExecute(Sender: TService); begin try Timer1.Enabled := True;//±Ò°Êtimer°õ¦æ¨Ã³q¹L´`Àô½T«OªA°È¬¡°Ê ·í°±¤îªA°È«á¡Atimer¸òÀH°±¤î while not Terminated do begin ServiceThread.ProcessRequests(True); end; Timer1.Enabled := False; finally end; end; end.