vba 如何将shell命令的输出保存到VBA中的文本文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12527201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:48:22  来源:igfitidea点击:

How to save the ouput of shell command into a text file in VBA

excelvbaexcel-vbaftp

提问by Mahe

I have my code written like this,

我的代码是这样写的,

  function ftpsend()

  Dim vPath As String
  Dim vFile As String
  Dim vFTPServ As String
  Dim fNum As Long
  Dim currntdir As String

  currntdir = ThisWorkbook.Path & "\"


 vPath = currntdir
 vFile = currntdir & "abc.xml"
 vFTPServ = "ftp.abc.com" 

  'Mounting file command for ftp.exe
  fNum = FreeFile()
 Open vPath & "abc.txt" For Output As #fNum
 Print #1, "USER student" 
 Print #1, "xxxxx"
 Print #1, "send " & vFile  
 Print #1, "close" 
 Print #1, "quit" 
 Close


 Shell "ftp -n -i -g -s:" & vPath & "abc.txt  " & vFTPServ, vbNormalFocus

 end function 

Now, the shell command displays the output in the console ftp.exe as

现在,shell 命令将控制台 ftp.exe 中的输出显示为

Connected to ftp.abc.com
220 Microsoft FTP service
ftp>USER student
230 User logged in 
ftp>send D:abc.xml
200 PORT command successful
226 Transfer Complete 
ftp>close
221>Good bye
ftp>

I want this output from console as it is copied into a text file.Because,sometimes when the username and password are incorrect, it displays the message as " User not logged in ", "Login failed" in the console.I want to handle that error.Any other way to handle ftp errors? Please Help...

我想要控制台的输出,因为它被复制到一个文本文件中。因为有时当用户名和密码不正确时,它会在控制台中显示消息“用户未登录”、“登录失败”。我想处理那个error.Any其他方法来处理ftp错误?请帮忙...

Thanks in advance

提前致谢

回答by Tony Dallimore

Try replacing

尝试更换

Shell "ftp -n -i -g -s:" & vPath & "abc.txt  " & vFTPServ, vbNormalFocus

by

经过

Shell "ftp -n -i -g -s:" & vPath & "abc.txt  " & vFTPServ & " > test.txt", vbNormalFocus

or

或者

Shell "ftp -n -i -g -s:" & vPath & "abc.txt  " & vFTPServ & " >> test.txt", vbNormalFocus

>or >>diverts console output to a file. Both >and >>will create test.txtif it does not exist. >will delete an existing text.txt while >>will append the new output to the existing contents of test.txt.

>>>将控制台输出转移到文件。双方>>>会创造test.txt,如果它不存在。 >将删除现有的 text.txt,同时>>将新输出附加到.txt的现有内容test.txt