Solved

VB Code write cmd.exe to file

Posted on 2013-06-13
7
407 Views
Last Modified: 2013-06-17
I have a process that works.

On a button click it (WIth hidden widnow)
Runs cmd.exe
executes a java command
The cmd.exe is using an append to file with > myfile.txt

The process runs...opens a connection to my vendor and starts receiving the inbound data

Now this is an open connection so as long as this runs I will get a continuous stream of telephone data on our network.
(No...I don't work for the NSA...LOL)

I allso have a process.kill() command.

WHat I want is for this to continuously append to the file

BUt it looks like the file isn't written until I kill the process?
Is that the way it works?  

I really need it going into the file in an ongoing manner

This is what is running
    Private Sub btnProcessStart_Click(sender As System.Object, e As System.EventArgs) Handles btnProcessStart.Click
        ' New ProcessStartInfo created
        Dim p As New ProcessStartInfo

        ' Specify the location of the binary
        p.FileName = "cmd.exe"
        p.WorkingDirectory = "C:\Program Files (x86)\Java\jre7\bin\"

        ' Use these arguments for the process
        p.Arguments = "/c java -cp m5.jar;m5-data-objects.jar;webservices-rt.jar;log4j-1.2.13.jar > C:\temp\Output3.txt"

        ' Use a hidden window
        p.WindowStyle = ProcessWindowStyle.Hidden

        ' Start the process
        Process.Start(p)

        Dim intResponse As Integer
        intResponse = MsgBox("End?", vbYesNoCancel, "Alert")
        If intResponse = vbYes Then
            For Each jr In Process.GetProcessesByName("Java")
                Try
                    
                    jr.Kill()
                
                Catch
                
                End Try
           
            Next
        End If
    End Sub

Open in new window

0
Comment
Question by:lrbrister
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
where's the code that writes to the file?
whats data do u wanna write to a file?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
Do not use >filename approach and redirect standard output instead as shown here

http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results

but instead of readtoend, use a while loop and do readline which should get you every line of cmd output
0
 

Author Comment

by:lrbrister
Comment Utility
CodeCruiser

On the StackFlow site...
Which of those solutions should I be using? I see 9 answers
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:angus_young_acdc
Comment Utility
The first one.

And the alteration will be something like this:

            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "YOURBATCHFILE.bat";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadLine();
            string line = string.Empty;
            while ((line = p.StandardOutput.ReadLine()) != null)
            {
                output = line;
            }
            p.WaitForExit();

Open in new window

0
 
LVL 15

Expert Comment

by:angus_young_acdc
Comment Utility
Sorry it's for VB!

        Dim p As New Process()
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.FileName = "YOURBATCHFILE.bat"
        p.Start()

        Dim output As String
        Dim line As String

        While ((line = p.StandardOutput.ReadLine()) != Nothing)
            output = line
        End While
        p.WaitForExit()

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
Comment Utility
The one with green tick is the one selection as final answer and is usually at the top of answers.
0
 

Author Closing Comment

by:lrbrister
Comment Utility
CodeCruiser
As always...thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now