locked
Process.Start with CMD

    Question

  • Hi there,

    I hope someone can help me out here. I start a few Processes like this:

            Dim di As New System.IO.DirectoryInfo(txtPath.Text)
            Dim fi() As System.IO.FileInfo
            For x As Integer = 0 To fi.GetUpperBound(0)
                    Process.Start("cmd.exe", "/c ftp -n -s:" & fi(x).FullName)
            Next

    For each File in some Directory, I start a process to get some files from a remote PC System. So is it possible, to loop through every Process and check, if the Process is still working on some FTP or has it done his work, and if so, close the process?

    Thanks for your help
    Lars
    Thursday, May 10, 2007 10:12 AM

Answers

  • The following allows you to run a DOS command, wait for it to finish and then capture the output and return.   It uses the System.diagnostics.process class but may provide you with what you want, so you dont suddenly get hundreds of processes all starting at almost the same time, rather you would get a single process created, when it ends and the next iteration runs you would get a different one created.  Resulting in only one FTP occuring at any time because of this loop.

     

    Code Snippet

       Public Function ProcessStartAndCaptureOutput(byval CmdFile as string, ByVal args As String) As String
            'Diagnostics.Process.Start(CmdFile, args)
            Dim p As Diagnostics.Process = New Diagnostics.Process
            p.StartInfo.FileName = CmdFile
            p.StartInfo.Arguments = args
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.Start()
            Return p.StandardOutput.ReadToEnd
            p.WaitForExit()
        End Function

     

    Hope that helps you.

     

    Thursday, May 10, 2007 6:09 PM

All replies

  • why not just use the FTP classes that are built in the framework versus launching a command line ftp utility?
    Thursday, May 10, 2007 1:10 PM
    Moderator
  • Store the Process object returned by the Process.Start() method and use it's StandardError, StandardInput, StandardOutput properties to provide input to the process and get output and error from the process.
    Thursday, May 10, 2007 1:12 PM
  • Yes, I tried that, and I get pretty far. It was working inside your Company House. But we have several buildings all over Germany and I can't get through our Company Firewall, and in fact I don't know how to do it. But if you have any suggestions about this, I really would appreciate it.
    Thursday, May 10, 2007 4:19 PM
  • The following allows you to run a DOS command, wait for it to finish and then capture the output and return.   It uses the System.diagnostics.process class but may provide you with what you want, so you dont suddenly get hundreds of processes all starting at almost the same time, rather you would get a single process created, when it ends and the next iteration runs you would get a different one created.  Resulting in only one FTP occuring at any time because of this loop.

     

    Code Snippet

       Public Function ProcessStartAndCaptureOutput(byval CmdFile as string, ByVal args As String) As String
            'Diagnostics.Process.Start(CmdFile, args)
            Dim p As Diagnostics.Process = New Diagnostics.Process
            p.StartInfo.FileName = CmdFile
            p.StartInfo.Arguments = args
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.Start()
            Return p.StandardOutput.ReadToEnd
            p.WaitForExit()
        End Function

     

    Hope that helps you.

     

    Thursday, May 10, 2007 6:09 PM
  • Thanks spotty. I really appriciate it.

    Lars
    Friday, May 11, 2007 11:21 AM