来自VBScript的Winscp - “get”不下载文件

时间:2016-01-07 20:18:18

标签: vbscript download ftp sftp winscp

我是Winscp和VBScript的新手,我需要一个脚本,从远程服务器下载文件名中包含昨天日期的文件。我尽我所能,最终创建了两个不同的脚本,这两个脚本似乎都有同样的问题。

从日志文件中,我可以看到Winscp打开目录,正确解释文件名模式,列出目录内容,但只是退出而不是获取指定的文件。我无法弄清楚出了什么问题。任何帮助表示赞赏!

以下是我写的2个脚本:

首先:

main
Sub main()

Dim mydate
Dim cmd1, cmd2_sftpcmd, cmd3, cmdActivity
Dim wshShell 
Set wshShell = WScript.CreateObject("WScript.Shell") 

'------------------------------ SET VARIABLES --------------

mydate = date - 1
MM_DD_YYYY =Right("00" & Month(mydate),2) &"-"& Right("00" & Day(mydate),2) & "-" & CSTR(Year(mydate))

cmd1="@echo off & cd C:/Program Files (x86)/WinSCP/ "
cmd2_sftpcmd = "winscp.exe /console /log=""E:/DATA/script/log_winscp_get_files.log"" /loglevel=0 /command ""open sftp://XXXX@XXXX.com -privatekey=""C:/putty/XXXX/xxxx.ppk"""" ""cd /exports"" ""option transfer binary"" ""get ""filename_" & MM_DD_YYYY & "_*.csv.gz"" E:/DATA/"" ""close"" ""exit"""
cmd3="exit" 
cmdActivity = "cmd /k " & cmd1 & " && " & cmd2_sftpcmd & " && " & cmd3 '


'------------------------------- RUN WScript Shell--------------
wshShell.Run cmdActivity

End Sub

第二

Function SFTPDownload()

Dim mydate, mmddyyyy, sFTPScript
Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

mydate = date - 1
mmddyyyy = Right("00" & Month(mydate),2) &"-"& Right("00" & Day(mydate),2) & "-" & CSTR(Year(mydate))

sFTPScript = sFTPScript & "option batch on" & vbCRLF
sFTPScript = sFTPScript & "option confirm off"& vbCrLf
sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
sFTPScript = sFTPScript & "open sftp://XXXX@XXXX.com -privatekey=C:/putty/XXXX/xxxx.ppk" & vbCrLf
sFTPScript = sFTPScript & "cd /exports" & vbCrLf
sFTPScript = sFTPScript & "get filename_" & mmddyyyy & "_*.csv.gz E:/DATA/" & vbCRLF
sFTPScript = sFTPScript & "close" & vbCrLf
sFTPScript = sFTPScript & "exit" & vbCrLf


sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

'Write the input file for the sftp command to a temporary file.
Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
oFTPScript.WriteLine(sFTPScript)
oFTPScript.Close
Set oFTPScript = Nothing  

sCmd = """C:\Program Files (x86)\WinSCP\winscp.exe""  /console /log=""E:/DATA/script/log_winscp_get_files.log"" /loglevel=1 -script=" & sFTPTempFile 
oFTPScriptShell.run sCmd

Wscript.Sleep 1000

' Get rid of temp file used for input to sftp
oFTPScriptFSO.DeleteFile(sFTPTempFile)

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function

SFTPDownload()

1 个答案:

答案 0 :(得分:0)

天哪!我才意识到我一直在寻找一个错误的日期模式(mm-dd-yyyy而不是yyyy-mm-dd)......这两个脚本实际上都有效。我想我会把它留在这里以防将来有人需要它。 干杯!