Question : How do I capture output from command prompt in vb.net?

Hi,

I am creating a vb.net script that will basically backup our Sharepoint Sites and Databases using both the STSADM and SMIGRATE command line tools.  I am also creating a log file to store information about the backup each day that it runs.  So, i need to know how i can use the Shell() function or some function to run the above mentioned command line tools and capture the output that is displayed on the dos screen.  Any help around this topic would be greatly appreciated...here's some of my code to show you where i'm at with it at the moment:

    Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        
        Dim fileStream As StreamReader
        Dim str_cmd, cmd_smigrate, cmd_smigrate_output_path, cmd_stsadm, cmd_stsadm_output_path As String
        Dim todaysDate, fileToExport, exportFolder, output, sharepoint_site_url, sharepoint_backup_sites As String
        Dim res_stsadm, res_smigrate As Integer

        todaysDate = Date.Today.Month.ToString & "_" & Date.Today.Day.ToString & "_" & Date.Today.Year.ToString
        fileToExport = "sharepoint_" & todaysDate & ".txt"

        exportFolder = "F:\Program Files\SharePoint Portal Server\Backup\Logs\" & fileToExport
        sharepoint_site_url = "stsadm -o enumsites -url http://sql-prod"
        sharepoint_backup_sites = Shell(sharepoint_site_url, AppWinStyle.NormalFocus)

        Dim fs As New FileStream(exportFolder, FileMode.Create, FileAccess.Write)
        Dim s As New StreamWriter(fs)
        s.BaseStream.Seek(0, SeekOrigin.End)
        s.WriteLine("Starting backup!")
        s.WriteLine()
        s.WriteLine("Backing site with command: " & sharepoint_site_url)
        s.WriteLine()
        s.WriteLine("Sites to be backed up: " & sharepoint_backup_sites)
        s.WriteLine()
        s.WriteLine()

        cmd_stsadm_output_path = "F:\Program Files\SharePoint Portal Server\Backup\sharepoint_sites.dat"
        cmd_stsadm = "stsadm -o backup -url http://sql-prod -filename " & cmd_stsadm_output_path & " - overwrite"
        res_stsadm = Shell(cmd_stsadm, AppWinStyle.NormalFocus, True, -1)

        cmd_smigrate_output_path = "C:\sql_prod_backup"
        cmd_smigrate = "smigrate -w http://sql-prod -f " & cmd_smigrate_output_path
        res_smigrate = Shell(cmd_smigrate, AppWinStyle.NormalFocus)

        Console.WriteLine()
        s.WriteLine(vbCrLf & vbCrLf & "Backup completed successfully!")
        s.Close()

    End Sub

As of right now the commands are running correctly to create my backup files but if you look at the following line of code:

sharepoint_backup_sites = Shell(sharepoint_site_url, AppWinStyle.NormalFocus)

...it returns some crazy number like 2992 or something...When the output on the command line screen is the following:


  http://sql-prod" Owner="GROUPSYNC\dbrinston" />
  http://sql-prod/personal/dbrinston" Owner="GROUPSYNC\dbrinston" />


So the above output from the command line is what i want to capture so that i can enter it in my log file.  Again any help would be appreciated.

Thanks,

Dave

Answer : How do I capture output from command prompt in vb.net?

From here:
How do I launch and monitor external programs from .NET?
http://www.thescarms.com/dotNet/Process.asp

'The following demonstrates how to run a BAT file and redirect the output to your program
        Dim oProcess As New Process
        'this is the bat file to run
        Dim s As String = "c:\q.bat"
        Dim sOutput As String

        oProcess.StartInfo.FileName = s
        oProcess.StartInfo.UseShellExecute = False
        'oProcess.StartInfo.CreateNoWindow = True
        oProcess.StartInfo.RedirectStandardOutput = True
        oProcess.Start()

        Dim sOut As System.IO.StreamReader = oProcess.StandardOutput

        oProcess.WaitForExit(10000) '10 seconds
        'grab the StdOut
        sOutput = sOut.ReadToEnd
        'clean up
        sOut.Close()
        oProcess.Close()
        'display the results
        Debug.WriteLine(sOutput)
Random Solutions  
 
programming4us programming4us