2013-07-19 10 views
0

我使用Visual Studio 2012並在VB中編寫。我想找到一種方法來啓動FTP下載或上傳的批處理文件,在CMD窗口中顯示進度並將傳輸時間的結果記錄到文本框中。使用VB,有沒有辦法從CMD窗口捕獲FTP結果?

任何幫助表示讚賞。

FTP批處理腳本如下:

文件名:FT​​P_1M_Download.bat:

@Echo off 
    cd (Folder name here) 
    ftp -s:FTP_1M_Download_s.bat 

文件名:FT​​P_1M_Download_s.bat:

open (FTP Address here) 
    username 
    password 
    hash 
    bin 
    be 
    get down_1M.zip 
+1

通過'WebRequest.Create()'使用'FtpWebRequest'。 – SLaks

回答

0

下面是一些未編輯的效用代碼我用於使用ProcessStartInfo運行RoboCopy。它捕獲輸出流(代碼在這種情況下不會執行任何操作)。

我使用Kellerman SFTP庫 - 效果很好,而且不貴。

Sub RunRobo(ByVal SD As String, ByVal DD As String, ByVal Job As String, ByRef Out As String, ByRef Err As String) 
    ' runs RoboCopy and returns log text 
    ' typical job: "*.* /V /NDL /S /E /COPY:DAT /PURGE /NP /R:0 /W:0 " 
    Try 
     Application.DoEvents() 
     Dim sParams As String = SD & " " & DD & " " & Job 
     'If Not ShowHeadFoot Then sParams &= " /NJH /NJS" 

     ' Set start information. 
     Dim start_info As New ProcessStartInfo("RoboCopy", sParams) 
     start_info.UseShellExecute = False 
     start_info.CreateNoWindow = True 
     start_info.RedirectStandardOutput = True 
     start_info.RedirectStandardError = True 

     ' Make the process and set its start information. 
     Dim proc As New Process() 
     proc.StartInfo = start_info 

     ' Start the process. 
     proc.Start() 

     ' Attach to stdout and stderr. 
     Dim std_out As System.IO.StreamReader = proc.StandardOutput() 
     Dim std_err As System.IO.StreamReader = proc.StandardError() 

     ' Display the results. 
     Out = std_out.ReadToEnd() 
     Err = std_err.ReadToEnd() 

     'Dim aLines() As String = sOut.Split(vbCrLf) ' if no header/footer creates array of file info 

     ' more generic would be to return via params 
     'txtStreetsErr.Text = sErr 
     'txtStreetsOut.Text = sOut 

     Try 
      std_out.Close() 
      std_err.Close() 
     Catch ex As Exception 

     End Try 
     Try 
      proc.Close() 
     Catch ex As Exception 

     End Try 

    Catch ex As Exception 
     MsgBox(ex.Message, , "Error during RoboCopy") 
    Finally 
     'Cursor = Cursors.Default 
    End Try 
End Sub 
+0

感謝您的幫助。但我一直陷在:Out = std_out.ReadToEnd() – Dam50fifty

+0

OUT和ERR是傳遞給實用程序Sub的ByRef變量。在代碼中聲明Out和Err作爲字符串,它應該可以工作。 – rheitzman

相關問題