Results 1 to 12 of 12

Thread: Append text to begining of text file

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Append text to begining of text file

    Hi,

    This runs the ping command and outputs the result to a text file called "Local Host.txt"

    Code:
            Shell("cmd /c ping 127.0.0.1 >C:\""Local Host.txt""", vbHide)
    How can I know if the file exists or not/ the command has been executed or not so that the next piece of code can be executed?
    PS: The next piece of code depends completely on the files, without it it is useless

    I tried this:

    Code:
            While IO.File.Exists("C:\Local Host.txt") = False
                Me.UseWaitCursor = True
            End While

    but, the next line of code gives me an error saying:

    The process cannot access the file 'C:\Local Host.txt' because it is being used by another process.

    PS: What I actually want to do is append certain text to the begining of the file (before the result of the command)

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Append text to begining of text file

    Well, unless your computer crashes or aliens blow it up with lasers the file must exist because it has to be in place for the output to be sent to it. It may not have any content but it certainly exists. That's the reason for the error in your present set-up. The file exists but it's still open for writing awaiting the output of the ping command.

    The logical thing to do here would be to use the VB.Net ping so that you can be sure a result is returned before the program proceeds. As long as you're using a separate process (ie. the cmd shell) there is no way to synchronise the threads. Example ...

    Dim p As New Net.NetworkInformation.Ping
    Dim px = p.Send("www.google.com")
    Me.Text = px.RoundtripTime.ToString
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: Append text to begining of text file

    Quote Originally Posted by dunfiddlin View Post
    Well, unless your computer crashes or aliens blow it up with lasers the file must exist because it has to be in place for the output to be sent to it. It may not have any content but it certainly exists. That's the reason for the error in your present set-up. The file exists but it's still open for writing awaiting the output of the ping command.

    The logical thing to do here would be to use the VB.Net ping so that you can be sure a result is returned before the program proceeds. As long as you're using a separate process (ie. the cmd shell) there is no way to synchronise the threads. Example ...

    Dim p As New Net.NetworkInformation.Ping
    Dim px = p.Send("www.google.com")
    Me.Text = px.RoundtripTime.ToString
    Thanks for replying back,
    I just used the Ping as an example.
    What If I want to run another command (not ping), and the output is sent to a text file, but I want the First line of that text file to be something else... ?
    How would I achieve that?

    I also tried using AppendText, but it also writes the text at the end of the file.

    For example:
    Code:
            Dim objStreamWriter As StreamWriter
            objStreamWriter = File.AppendText("C:\NewText.txt")
            objStreamWriter.WriteLine("Hello World")
            objStreamWriter.Close()
    Assuming the Text file "NewTest.txt" contains text already and I want to add the text "Hello World" at the begining of the .txt File, What would I need to do to achieve it?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Append text to begining of text file

    Well, not to be too obvious about it, write what you want first, first! If you can't do that then read the file into a string. Add the new text, and rewrite the file with the extended string.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: Append text to begining of text file

    I tried out what you said, and came up with this:

    Code:
            Shell("cmd /c ping 127.0.0.1 >C:\LocalHost.txt", vbHide)
    
            While IO.File.Exists("C:\LocalHost.txt") = False
                ProgressBar1.Value = 50
    	'Using the progress bar to determine wether the text file exists or not.
    
            End While
    
            ProgressBar1.Value = 99
    
            Dim storage As String = File.ReadAllText("C:\LocalHost.txt")
            'An error is thrown here:
            'The process cannot access the file 'C:\LocalHost.txt' because it is being used by another process.
    
            Dim w As New IO.StreamWriter("C:\LocalHost.txt")
            w.WriteLine("This is a sample test for a log file")
            w.WriteLine("Testing.....")
            w.WriteLine("Testing again.....")
            w.WriteLine("Testing once again.........")
            w.WriteLine(storage)
            'w.WriteLine(storage)
            w.Close()

    An example would be really helpful...

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Append text to begining of text file

    vb.net Code:
    1. Public Class Form1
    2.     Dim WithEvents p As New Process With {.EnableRaisingEvents = True}
    3.  
    4.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    5.         p.StartInfo.UseShellExecute = False
    6.         p.StartInfo.FileName = "cmd"
    7.         p.StartInfo.Arguments = "/c ping 127.0.0.1 >C:\""Local Host.txt"""
    8.         p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    9.         p.Start()
    10.         p.WaitForExit()
    11.         Dim s As String = IO.File.ReadAllText("C:\Local Host.txt")
    12.         s = "Ping Local Host " & Now.ToShortDateString & vbCrLf & s
    13.         IO.File.WriteAllText("C:\Local Host.txt", s)
    14.     End Sub
    15. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: Append text to begining of text file

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. Public Class Form1
    2.     Dim WithEvents p As New Process With {.EnableRaisingEvents = True}
    3.  
    4.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    5.         p.StartInfo.UseShellExecute = False
    6.         p.StartInfo.FileName = "cmd"
    7.         p.StartInfo.Arguments = "/c ping 127.0.0.1 >C:\""Local Host.txt"""
    8.         p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    9.         p.Start()
    10.         p.WaitForExit()
    11.         Dim s As String = IO.File.ReadAllText("C:\Local Host.txt")
    12.         s = "Ping Local Host " & Now.ToShortDateString & vbCrLf & s
    13.         IO.File.WriteAllText("C:\Local Host.txt", s)
    14.     End Sub
    15. End Class


    Awesome....
    The code that you provided does exactly what I wanted to do.
    Just a very teeny bit of problem, if it's not too much to ask, Is there a way to hide the Command Window?

    I expected this line of code to do it but it doesn't, the command window shows up (empty, no text... (at least...))
    Code:
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    I tried this too:
    Code:
    p.StartInfo.WindowStyle = vbHidden
    still the same.

    Thanks a lot...

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Append text to begining of text file

    Try ....

    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized

    Seems the window needs to be open for a bit to process the command so this will at least keep it out of sight.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: Append text to begining of text file

    Oh well... You know what they say, Something is better than nothing..
    Thanks a lot, once again...


    EDIT:
    Code:
    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
    The window won't stay minimized, as long as it does what I want it to, I'm happy... unless there is another way to keep it minimized (which I doubt)
    Last edited by TheThinker; Jan 6th, 2013 at 05:02 AM.

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Append text to begining of text file

    Hmm. Did for me but, as you say, as long as it works. Come to think of it, MS doesn't seemed to have solved this either as the cmd window frequently (if fleetingly) appears when running from the Run dialog.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Append text to begining of text file

    Quote Originally Posted by TheThinker View Post
    The window won't stay minimized, as long as it does what I want it to, I'm happy... unless there is another way to keep it minimized (which I doubt)
    Instead of setting the WindowStyle try setting CreateNoWindow to True.
    Using, p.StartInfo.CreateNoWindow = True, I don't see the cmd window at all in Win7.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: Append text to begining of text file

    Quote Originally Posted by Edgemeal View Post
    Instead of setting the WindowStyle try setting CreateNoWindow to True.
    Using, p.StartInfo.CreateNoWindow = True, I don't see the cmd window at all in Win7.
    Perfecto!!!


    Totally works in Windows 7,
    Thanks a ton, again...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width