2012-09-21 57 views
0

我寫這樣我的代碼,如何shell命令的輸出中保存到一個文本文件中VBA

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錯誤?請幫助...

在此先感謝

回答

0

嘗試通過

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 

>>>格更換

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

erts控制檯輸出到文件。如果它不存在,則>>>將創建test.txt>將刪除現有的text.txt,而>>將把新的輸出附加到test.txt的現有內容。

+0

對不起!它沒有工作。它只是在test.txt文件中只放置了「ftp>」這一行。我的所有輸出都不會被複制到文本文件中。請使用其他任何解決方案! – Mahe

+0

你在使用'>'嗎?考慮過你的輸出後,我的猜測是每一行都算作一個單獨的輸出。用'>'每一行都會刪除現有文件並創建一個新文件。結果'test.txt'只包含最後一行「ftp>」。試試'>>'。 –

+0

嘗試使用組合'>'和'>>'。以這種方式得到 Shell「ftp.exe -n -i -g -s:」&vPath&「\ abc.txt」&vFTPServ&「>> Test.txt」,vbNormalFocus 相同的輸出結果他們無法找到爲什麼只有最後一行被抄襲的原因! – Mahe

相關問題