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

时间:2012-09-21 08:49:57

标签: excel vba excel-vba ftp

我的代码写得像这样,

  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 

现在,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>

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

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试替换

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

通过

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

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

>>>将控制台输出转移到文件。如果>>>不存在,test.txt>都会创建>>test.txt会删除现有的text.txt,而{{1}}会将新输出附加到{{1}}的现有内容。