vbscript - 如何从 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 Files\WinSCP\WinSCP.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:\Websites\SFTP\put.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
如果该文件不存在,您的代码将在此处退出。
另一件事是您的 Function 获取您应该在函数中使用的参数(sSite
、sUsername
、sPassword
等)。相反,您会sRemotePath
立即覆盖sLocalFile
。这可能是因为您正在排除故障,但似乎也没有使用其他参数。
以下行中的引号还有其他问题:
sFTPScript = sFTPScript & "open sftp://" & buildsftp & ":" & 9D2GRGCu & "@" & sftp-qa.ebs.thomsonreuters.com & vbCrLf
sFTPScript = sFTPScript & "cd " & \\sftp-qa.ebs.thomsonreuters.com\dummy\ldapfulltab.txt & vbCrLf
sFTPScript = sFTPScript & "put " & C:\Websites\SFTP\put.txt & vbCRLF
至少应该是这样的,但你应该在这里使用你的参数:
sFTPScript = sFTPScript & "open sftp://" & buildsftp & ":" & "9D2GRGCu" & "@" & "sftp-qa.ebs.thomsonreuters.com" & vbCrLf
sFTPScript = sFTPScript & "cd " & "\\sftp-qa.ebs.thomsonreuters.com\dummy\ldapfulltab.txt" & vbCrLf
sFTPScript = sFTPScript & "put " & sLocalFile & vbCrLf
推荐阅读
- ios - NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9860)
- python - 选择带有 beautifulsoup 点的标签
- javascript - Modal 没有在我的 react-native 应用程序中关闭?
- angular - 覆盖特定文件的角度加载器
- javascript - 如何将计算值复制到剪贴板?
- amazon-web-services - 如何构建文件的无服务器转换
- php - 单击下个月时创建事件
- javascript - 如何从 css 使用 jQuery 控制动画时间
- c# - 如何保持图表中的间隔?在 Designer 中,必要的代码行不断消失
- flutter - 更改 DropDownButton 选择时出现颤振错误