#include <FTPEx.au3>
#include <Array.au3>
Local $path
$server = '' ;服务器
$name = '' ;用户名
$pass = '' ;密码
$Port = '2121' ;端口
$path = "c:\ActiveX" ;路径
$RemoteDir = $path ;远程根路径路径
$LocalDir = "D:\存储软件\au3\进行中\FTP同步\实验室1\同步路径" ;本地路径
ftp($server, $name, $pass, $Port, $path, $RemoteDir)
Func ftp($server, $name, $pass, $Port, $path, $RemoteDir)
$Open = _FTP_Open('MyFTP') ;打开一个FTP会话
$Conn = _FTP_Connect($Open, $server, $name, $pass, 0, $Port) ;连接到ftp服务器
_FTP_DirSetCurrent($Conn, $path)
$aFile = _FTP_ListToArrayEx($Conn, 0, 0) ;列目录(0=文件和目录,1=目录,2=文件)(格式0=mm/dd/yyyy,1=yyyy/mm/dd)
;~ _ArrayDisplay($aFile)
;~ MsgBox(0,"显示","共(" & $aFile[0][0] & ")个")
;~ MsgBox(0,"$aFile[$i][0个]文件名",$aFile[0][0])
For $i = 1 To $aFile[0][0]
If $aFile[$i][2] = 16 And $aFile[$i][0] <> "." And $aFile[$i][0] <> ".." Then
;~ $path=$path&$aFile[$i][0]&""
;~ MsgBox(0, "$path", $path & $aFile[$i][0] & "", $RemoteDir)
ftp($server, $name, $pass, $Port, $path & $aFile[$i][0] & "", $RemoteDir)
EndIf
If $aFile[$i][0] <> "." And $aFile[$i][0] <> ".." And $aFile[$i][2] <> 16 Then
;检验
;~ MsgBox(0,"",$aFile[$i][0])
$fname = $aFile[$i][0]
$sync = ftpsync($LocalDir, $RemoteDir, $path, $fname)
;~ MsgBox(0,"是否存在文件返回信息",$sync)
;~ If $fname = "PiView.mdb" Then
;~ MsgBox(0,"是否存在文件返回信息",$sync)
;~ EndIf
Select
Case $sync = 1
$ftp_download = _FTP_FileGet($Conn, $path & $fname, $sync, False) ;下载文件(True=覆盖 False=不覆盖(默认))
;~ MsgBox(0,$ftp_download,$path & $aFile[$i][0] &"-"& $sync)
Case $sync <> 1 Or $sync <> 0
$ftpsize = _FTP_FileGetSize($Conn, $path & $aFile[$i][0])
$localsize = FileGetSize($sync)
;~ MsgBox(0, "大小对比", $ftpsize & "-" & $localsize)
If $ftpsize <> $localsize Then
;~ MsgBox(0, "大小对比不成功", $ftpsize & "-" & $localsize)
;~ MsgBox(0, "大小对比不成功", $path & $aFile[$i][0]& "-" & $sync)
$ftp_download = _FTP_FileGet($Conn, $path & $aFile[$i][0], $sync, False)
;~ MsgBox(0,"$ftp_download",$ftp_download)
EndIf
Case Else
MsgBox(0, "", "搜索失败")
EndSelect
;~ MsgBox(0,"次数",$aFile[$i][0])
EndIf
;~ MsgBox(0,"显示","名 = " & $aFile[$i][0] & @CRLF & $aFile[$i][1] & @CRLF & $aFile[$i][2] & @CRLF & $aFile[$i][3] & @CRLF & $aFile[$i][4] & @CRLF & $aFile[$i][5])
Next
;$aFile[$i][0个]文件名
;$aFile[$i][1]文件大小
;$aFile[$i][2]文件属性
;$aFile[$i][3]文件修改日期-时间
;$aFile[$i][4]文件创作日期-时间
;$aFile[$i][5]文件通入日期-时间
$Ftpc = _FTP_Close($Open) ;关闭 _FTP_Open 打开的回话
EndFunc ;==>ftp
;~ Func Download
;~ 用途:下载一个FTP上的文件
;~ $ftp_download = _FTP_FileGet($Conn, "111\test222.txt", "e:\au3_ftp\test222.txt", False) ;下载文件(True=覆盖 False=不覆盖(默认))
;~ EndFunc ;==>ftp
Func ftpsync($LocalDir, $RemoteDir, $path, $fname)
If FileExists($LocalDir) Then
Else
DirCreate($LocalDir)
EndIf
Local $len = StringLen($RemoteDir)
Local $len1 = StringLen($path)
$New = StringRight($path, $len1 - $len)
If FileExists($LocalDir & $New) Then
Else
;创建目录
DirCreate($LocalDir & $New)
EndIf
If $fname = "PiView.mdb" Then
MsgBox(0,"文件是否存在判定",$LocalDir & $New & $fname)
MsgBox(0,"$path",$path)
EndIf
If FileExists($LocalDir & $New & $fname) Then
;判断文件大小
Return $LocalDir & $New & $fname
Else
;创建目录下载文件
;~ MsgBox(0,"文件是否存在判定",$LocalDir & $New & $fname)
Return 1
EndIf
EndFunc ;==>ftpsync