VBScript操作SFTP

示例代码主要通过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

 

转载于:https://www.cnblogs.com/panchunting/p/VBScript_SFTP.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值