首页 > 代码库 > VBScript操作SFTP

VBScript操作SFTP

示例代码主要通过VBScript实现对SFTP的上传下载功能

 Return yyyyMM base on current dateFunction FormatCurrentDate()    CurrentDate = Formatdatetime(Date,2)    FormatCurrentDate = right("0000" & year(CurrentDate),4) & right("00" & month(CurrentDate),2)End Function Upload and Backup files in given folderSub 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    NextEnd Sub Download files from remote SFTPSub 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 SubDim MySite, fos, outFileOn Error Resume NextSet 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 objectSet MySite = CreateObject("CuteFTPPro.TEConnection") Initialize remote server host name, protocol, port, etc.MySite.Host = "xx.xx.xx.xx"MySite.Protocol = "SFTP"MySite.Port = 22MySite.Retries = 30MySite.Delay = 30MySite.MaxConnections = 2MySite.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 serverMySite.DisconnectMySite.ConnectIf Cbool(MySite.IsConnected) Then    outFile.WriteLine "Connected to server: " & MySite.HostEnd IfIf Err.Number > 0 Then    outFile.WriteLine "Error: " & Err.Description    Err.ClearEnd IfCall 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/") CloseoutFile.WriteLine "End Time: " & FormatDateTime(Now())outFile.WriteLine "==========================================================================================" & vbCrLf & vbCrLfoutFile.CloseSet fos = NothingMySite.DisconnectMySite.CloseWScript.Quit

最后建一计划任务,每隔30分钟轮询执行

schtasks /create /sc minute /mo 30 /tn "CUSDECEDI" /tr E:\CUSDEC\CUSDEC.vbs /ru System

 

VBScript操作SFTP