捕获WScript Shell对象的输出

时间:2018-05-31 13:59:58

标签: excel-vba ftp putty vba excel

此代码使用Putty

将文件上传到FTP服务器
Dim wsh As Object
Dim waitOnReturn As Boolean
Dim windowStyle As Integer
Dim cstrSftp As String
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
Dim site As String
Dim resp As String

Set wsh = VBA.CreateObject("WScript.Shell")

'Wait the execution to finish
waitOnReturn = True

'Show the window
windowStyle = 1

'Variables
cstrSftp = """" & Application.ActiveWorkbook.Path & "\pscp.exe" & """"
site = "http://mysite/"
pUser = "user"
pPass = "password  "
pHost = "ftp.mysite"
pRemotePath = "/home/"
pFile = """" & Application.ActiveWorkbook.Path & "file.png" & """"

'Command string
strCommand = "cmd /c echo n | " & cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & " " & pFile & " " & pHost & ":" & pRemotePath

'Run the command
wsh.Run strCommand, windowStyle, waitOnReturn

由于存储服务器不可靠,我需要捕获输出以了解上传是否有效。并且,如果它不是我需要知道什么是消息。

我想过使用命令" > C:\ output.txt的"捕获输出。喜欢这个

strCommand = strCommand & " > " & """" "C:\output.txt" & """"

当上传工作时,我的输出文件也可以正常工作。但是,当上传不起作用时,输出文件中不会写入任何内容。

例如,当我收到消息Fatal: Server unexpectedly closed network connection时,输出文件中没有写入任何内容。但我需要知道给出的确切消息是什么。

1 个答案:

答案 0 :(得分:2)

使用exec检索输出的示例,这可能在您的情况下很有用

Sub TestExec()

Dim wsh As New WshShell
Dim s As String

    s = wsh.Exec("cmd /c Dir C:\ ").StdOut.ReadAll
    Debug.Print s

End Sub