示例代码主要通过VBScript实现对SFTP的上传下载功能
' Return yyyyMM base on current date Function FormatCurrentDate() CurrentDate = Formatdatetime(Date,2) FormatCurrentDate = right("0000" & year(CurrentDate),4) & right("00" & month(CurrentDate),2) End Function ' Upload and Backup files in given folder Sub UploadAndBackupFiles(MySite,fs,outFile,localFolder,bakLocalFolder) Dim oFolder,oFiles, fileName set oFolder = fs.GetFolder(localFolder) set oFiles = oFolder.Files for each file in oFiles fileName = file.Name ' Check bak folder, if not exist, then create it If(Not(MySite.LocalExists(bakLocalFolder))) Then MySite.CreateLocalFolder bakLocalFolder End If If(MySite.LocalExists(localFolder & fileName)) Then ' Upload file If(Not(MySite.RemoteExists("/inbound/" & fileName))) Then outFile.WriteLine FormatDateTime(Now()) & ": [UPLOAD] " & fileName MySite.Upload localFolder & fileName, "/inbound/" & fileName End If ' Backup file to bak folder outFile.WriteLine FormatDateTime(Now()) & ": [BACKUP] " & fileName MySite.LocalRename localFolder & fileName, bakLocalFolder & fileName End If Next End Sub ' Download files from remote SFTP Sub DownloadFilesFromSFTP(MySite,outFile,localFolder,remoteFolder) Dim strFileList,strFileName,i,j MySite.LocalFolder = localFolder MySite.RemoteFolder = remoteFolder If CBool(MySite.RemoteExists(MySite.RemoteFolder)) Then If CBool(MySite.LocalExists(MySite.LocalFolder)) Then ' 获取远程下载目录的文件列表,以"|||"作为分隔符 MySite.GetList "", "", "%NAME|||" strFileList = MySite.GetResult If Len(strFileList) <> 0 Then i = 1 Do While true j = InStr(i, strFileList, "|||") If j <= 0 Then Exit Do End If strFileName = Mid(strFileList, i, j - i) outFile.WriteLine FormatDateTime(Now()) & ": [DOWNLOAD] " & strFileName MySite.Download strFileName outFile.WriteLine FormatDateTime(Now()) & ": [*REMOVE*] " & strFileName MySite.RemoteRemove strFileName '加5,因为分隔符"|||"的长度为3个字符,另外还有回车和换行2个字符 i = j + 5 Loop Else outFile.WriteLine "Message! There is no file in remote sftp" End If Else outFile.WriteLine "Error! Local directory doesn't existing" End If Else outFile.WriteLine "Error! Remote directory doesn't existing" End If End Sub Dim MySite, fos, outFile On Error Resume Next Set fos = WScript.CreateObject("scripting.filesystemobject") Set outFile = fos.OpenTextFile("\\10.7.11.103\cusdec_edi\Schedule\VBS_CUSDEC_Log.txt", 8, True) outFile.WriteLine "==========================================================================================" outFile.WriteLine "Start Time: " & FormatDateTime(Now()) ' Create TEConnection object Set MySite = CreateObject("CuteFTPPro.TEConnection") ' Initialize remote server host name, protocol, port, etc. MySite.Host = "xx.xx.xx.xx" MySite.Protocol = "SFTP" MySite.Port = 22 MySite.Retries = 30 MySite.Delay = 30 MySite.MaxConnections = 2 MySite.TransferType = "AUTO" MySite.DataChannel = "DEFAULT" MySite.AutoRename = "OFF" ' WARNING!!! SENSITIVE DATA: user name and password. MySite.Login = "user" MySite.Password = "pwd" MySite.SocksInfo = "" MySite.ProxyInfo = "" ' Connect to remote server 'MySite.Disconnect MySite.Connect If Cbool(MySite.IsConnected) Then outFile.WriteLine "Connected to server: " & MySite.Host End If If Err.Number > 0 Then outFile.WriteLine "Error: " & Err.Description Err.Clear End If Call UploadAndBackupFiles(MySite,fos,outFile,"\\10.7.11.103\cusdec_edi\ZB1\","\\10.7.11.103\cusdec_edi\Archive\ZB1\" & FormatCurrentDate() & "\") Call UploadAndBackupFiles(MySite,fos,outFile,"\\10.7.11.103\cusdec_edi\ZB2\","\\10.7.11.103\cusdec_edi\Archive\ZB2\" & FormatCurrentDate() & "\") Call DownloadFilesFromSFTP(MySite,outFile,"\\10.7.11.103\cusdec_edi\ZBack","/outbound/") ' Close outFile.WriteLine "End Time: " & FormatDateTime(Now()) outFile.WriteLine "==========================================================================================" & vbCrLf & vbCrLf outFile.Close Set fos = Nothing MySite.Disconnect 'MySite.Close WScript.Quit
最后建一计划任务,每隔30分钟轮询执行
schtasks /create /sc minute /mo 30 /tn "CUSDECEDI" /tr E:\CUSDEC\CUSDEC.vbs /ru System