如何从SFTP服务器获取文本文件并在VBScript中本地存储
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从SFTP服务器获取文本文件并在VBScript中本地存储相关的知识,希望对你有一定的参考价值。
我正在尝试从SFTP服务器获取文本文件并将其存储在我的本地容器中。
注意:VBScript中需要代码。这是商业版本。我已经尝试过
Function MISC_FTPUpload(byVal sSite, byVal sUsername, byVal sPassword, byVal sLocalFile, byVal sRemotePath, byRef sError)
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2
Dim oFTPScriptFSO
Dim oFTPScriptShell
Dim sOriginalWorkingDirectory
Dim sFTPScript
Dim sFTPTemp
Dim bRetCode
Dim sFTPTempFile
Dim oFTPScript
Dim sResults
Dim sOut
Dim sCmd
LOG_Write "MISC_FTPUpload called at: " & Now
Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")
sRemotePath = Trim(remothpath)
sLocalFile = Trim(localpath)
If InStr(sRemotePath, " ") > 0 Then
If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
sRemotePath = """" & sRemotePath & """"
End If
End If
If InStr(sLocalFile, " ") > 0 Then
If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then
sLocalFile = """" & sLocalFile & """"
End If
End If
If Len(sRemotePath) = 0 Then
'Please note that no premptive checking of the
'remote path is done. If it does not exist for some
'reason, Unexpected results may occur.
sRemotePath = ""
End If
If InStr(sLocalFile, "*") Then
If InStr(sLocalFile, " ") Then
sError = "Error: Wildcard uploads do not work if the path contains a space." & vbNewLine & "This is a limitation of the Microsoft FTP client."
LOG_Write sError
MISC_FTPUpload = False
Exit Function
End If
ElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then
sError = "Error: File Not Found."
LOG_Write sError
MISC_FTPUpload = False
Exit Function
End If
sFTPScript = sFTPScript & "option batch on" & vbCRLF
sFTPScript = sFTPScript & "option confirm off"& vbCrLf
sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
sFTPScript = sFTPScript & "open sftp://" & sftpuser & ":" & passsftp & "@" & sftphostname & vbCrLf
sFTPScript = sFTPScript & "cd " & sftp server path & vbCrLf
sFTPScript = sFTPScript & "put " & localpath & vbCRLF
sFTPScript = sFTPScript & "close" & vbCrLf
sFTPScript = sFTPScript & "exit" & vbCrLf
LOG_Write "Script for FTP File: " & vbNewLine & sFTPScript
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "" & oFTPScriptFSO.GetTempName
LOG_Write "FTP Input file stored at: " & sFTPTempFile
Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
oFTPScript.WriteLine(sFTPScript)
oFTPScript.Close
Set oFTPScript = Nothing
sCmd = """C:Program FilesWinSCPWinSCP.com"" -script=" & sFTPTempFile
MISC_RunCmd sCmd, sOut, sError
LOG_Write sOut
Wscript.Sleep 1000
oFTPScriptFSO.DeleteFile(sFTPTempFile)
If sError = "" And InStr(sOut, "binary") >0 And InStr(sOut, "100%") >0 Then
MISC_FTPUpload = True
Else
sError = "Error: " & sError
LOG_Write sError
MISC_FTPUpload = False
End If
Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function`
但是我没有得到任何回应。
答案
您脚本中的一个重要问题是,它希望C:WebsitesSFTPput.txt
文件在下载之前在本地存在:
ElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then
sError = "Error: File Not Found."
LOG_Write sError
MISC_FTPUpload = False
Exit Function
End If
如果文件不存在,您的代码将在这里退出。
另一件事是您的函数获得了应在函数中使用的参数(sSite
,sUsername
,sPassword
等)。相反,您会立即覆盖sRemotePath
和sLocalFile
。可能是因为您在进行故障排除,但似乎也未使用其他参数。
以下行中的引号还有其他问题:
sFTPScript = sFTPScript & "open sftp://" & buildsftp & ":" & 9D2GRGCu & "@" & sftp-qa.ebs.thomsonreuters.com & vbCrLf
sFTPScript = sFTPScript & "cd " & \sftp-qa.ebs.thomsonreuters.comdummyldapfulltab.txt & vbCrLf
sFTPScript = sFTPScript & "put " & C:WebsitesSFTPput.txt & vbCRLF
至少应该是这样,但是您应该在这里使用参数:
sFTPScript = sFTPScript & "open sftp://" & buildsftp & ":" & "9D2GRGCu" & "@" & "sftp-qa.ebs.thomsonreuters.com" & vbCrLf
sFTPScript = sFTPScript & "cd " & "\sftp-qa.ebs.thomsonreuters.comdummyldapfulltab.txt" & vbCrLf
sFTPScript = sFTPScript & "put " & sLocalFile & vbCrLf
以上是关于如何从SFTP服务器获取文本文件并在VBScript中本地存储的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Pentaho Kettle 读取所有文件夹和子文件夹 使用 SFTP 步骤获取文件
我使用的是SecureCRTP来连接linux,请问如何把文件从本地电脑上传到linux服务器上?具体点,谢谢