我编写了下面的代码,试图通过ftp和一个通过sftp上传到两个不同的服务器。通过sFTP&FTP上传VBA,通过日志输出检测错误
我想知道是否有更好的方式通过SFTP上传,因为当前的方法,因为我有它不会触发FTP错误,如果它失败的任何部分。
我想一个解决办法,我想要的是他们两个将输出记录到文本文件,然后从中我可以看到错误是手动的,如果我想设置一个简单的读取日志,检查错误,如果x做Y ...
On Error GoTo Err_FTPFile
' UPLOAD FIRST FILE VIA FTP
'Build up the necessary parameters
sHost = "ftp.server.com"
sUser = "[email protected]"
sPass = "password"
sSrc = """" + Environ("TEMP") + "\" + file + ".txt" + """"
sDest = "/remote/folder/"
'Write the FTP commands to a file
iFNum = FreeFile
sFTPCmds1 = Environ("TEMP") & "\" & "FTPCmd1.tmp"
Open sFTPCmds1 For Output As #iFNum
Print #iFNum, "ftp"
Print #iFNum, "open " & sHost
Print #iFNum, sUser
Print #iFNum, sPass
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "disconnect"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Shell Environ("WINDIR") & "\System32\ftp.exe -s:" & sFTPCmds1
Application.Wait (Now + TimeValue("0:00:10"))
' UPLOAD SECOND FILE VIA SFTP
'Build up the necessary parameters
sFTPDetails = "C:\psftp.exe -b C:\commands.tmp [email protected] -pw password"
sSrc = """" + Environ("TEMP") + "\" + file + ".txt" + """"
sDest = "/remote/folder/"
'Write the FTP commands to a file
iFNum = FreeFile
sFTPCmds2 = sFolder & "\" & "commands.tmp"
Open sFTPCmds2 For Output As #iFNum
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "quit"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Call Shell(sFTPDetails, vbNormalFocus)
Application.Wait (Now + TimeValue("0:00:10"))
Exit_FTPFile:
On Error Resume Next
Close #iFNum
'Delete the temp FTP command file
Kill sFTPCmds1
Kill sFTPCmds2
Kill Environ("TEMP") + file + ".txt"
GoTo ContinuePoint
Err_FTPFile:
Shell "C:\FailPushBullet.exe"
MsgBox Err.Number & " - " & Err.Description & " Failed.", vbOKOnly, "Error"
GoTo ContinuePoint
ContinuePoint:
' Do stuff
我非常想在底部的SFTP一个工作和功能完全一样的FTP一个从上面。
我尝试以下,这运行:
sClient = "C:\psftp.exe"
sArgs = "[email protected] -pw passexample -b C:\commands.tmp"
sFull = sClient & " " & sArgs
sSrc = """" + Environ("TEMP") + "\" + "test" + ".txt" + """"
sDest = "folder"
'Write the FTP commands to a text file
iFNum = FreeFile
sFTPCmds = "C:\" & "commands.tmp"
Open sFTPCmds For Output As #iFNum
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "quit"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Call Shell(sFull, vbNormalFocus)
但是,如果我改变对个SARG它sArgs = "[email protected] -pw passexample -b C:\commands.tmp 1> log.txt"
不运行,它只是关闭没有做任何事情。我以为1> log.txt
应该将输出到文件
与您的问题没有直接关系,但您应该使用'Resume'来退出错误处理块,而不是'GoTo'。像这样使用'GoTo'意味着你不能在该点之后使用任何错误处理。 – Kyle
@Kyle我可以在下一个goto下使用它吗? – Ryflex
@Kyle是正确的 - 通过在这里使用GoTo,你不能在子程序中进行任何进一步的错误处理,因为Excel认为从该点开始的所有代码都是初始错误处理的一部分。清楚的是,这意味着如果在“ContinuePoint”引用之后遇到另一个错误,Excel将无法正确处理它。如果将行更改为“Resume ContinuePoint”,那么您将有适当的错误处理。有关错误处理的更多信息,请参阅http://www.cpearson.com/excel/errorhandling.htm(请注意“简历声明”部分)。 –