我想将其更改为 SFTP 服务器。我应该如何更新以下脚本?
'FTP Upload
'Upload a file/folder to an FTP server
Set oShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFSO
Varnow = now
vardate = Day(varnow) & "-" & Month(varnow) & "-" & Year(varnow) & ".csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "D:/file.csv"
strRename = "D:/file-" & vardate
If objFSO.FileExists(strFile) Then
objFSO.MoveFile strFile, strRename
End If
'Path to file or folder to upload
path = strRename
FTPUpload(path)
Sub FTPUpload(path)
On Error Resume Next
'Copy Options: 16 = Yes to All
Const copyType = 16
'FTP Wait Time in ms
waitTime = 8000
FTPUser = "ftuser"
FTPPass = "psw"
FTPHost = "hostname"
FTPDir = "/Dir"
strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost & FTPDir
Set objFTP = oShell.NameSpace(strFTP)
'Upload single file
If objFSO.FileExists(path) Then
Set objFile = objFSO.getFile(path)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)
Set objItem = objFolder.ParseName(objFile.Name)
objFTP.CopyHere objItem,copyType
End If
If Err.Number <> 0 Then
Wscript.Echo "Error: " & Err.Description
End If
'Wait for upload
WScript.Sleep waitTime
If objFSO.FileExists(strRename) Then
objFSO.MoveFile strRename, strFile
End If
End Sub
如你所见,我想将FTP上传切换为SFTP上传(因为目标服务器已更改为SFTP协议(protocol)。在这个函数中具体应该修改什么Sub FTPUpload(path)
这样做?
您有什么建议吗?
最佳答案
FileSystemObject
不支持 SFTP,Windows 中也不支持任何其他方式。
您必须使用第三方 SFTP 命令行客户端或 COM 对象。
因此没有简单的方法来更改现有脚本。您基本上必须从头开始。
Stack Overflow 上存在很多关于使用 VBScript 进行 SFTP 上传的现有问题。
例如,您可以使用 WinSCP SFTP client 。它同时具有 command-line scripting interface和 COM object .
- Using WinSCP scripting in Windows Scripting host (JScript or VBScript)
不需要任何安装,但从VBS使用起来比较困难。 - Using WinSCP .NET assembly/COIM library in Windows Scripting host (JScript or VBScript)
需要安装(注册),但从 VBS 使用更容易。
(我是 WinSCP 的作者)
关于file-upload - 如何将 FTP 上传 VBScript 更改为 SFTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48204370/