|
-
Jan 4th, 2013, 04:39 PM
#1
Thread Starter
Member
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)
-
Jan 4th, 2013, 04:57 PM
#2
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!
-
Jan 4th, 2013, 06:02 PM
#3
Thread Starter
Member
Re: Append text to begining of text file
 Originally Posted by dunfiddlin
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?
-
Jan 4th, 2013, 06:13 PM
#4
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!
-
Jan 5th, 2013, 05:16 AM
#5
Thread Starter
Member
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...
-
Jan 5th, 2013, 11:57 AM
#6
Re: Append text to begining of text file
vb.net Code:
Public Class Form1
Dim WithEvents p As New Process With {.EnableRaisingEvents = True}
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
p.StartInfo.UseShellExecute = False
p.StartInfo.FileName = "cmd"
p.StartInfo.Arguments = "/c ping 127.0.0.1 >C:\""Local Host.txt"""
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()
p.WaitForExit()
Dim s As String = IO.File.ReadAllText("C:\Local Host.txt")
s = "Ping Local Host " & Now.ToShortDateString & vbCrLf & s
IO.File.WriteAllText("C:\Local Host.txt", s)
End Sub
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!
-
Jan 5th, 2013, 01:01 PM
#7
Thread Starter
Member
Re: Append text to begining of text file
 Originally Posted by dunfiddlin
vb.net Code:
Public Class Form1 Dim WithEvents p As New Process With {.EnableRaisingEvents = True} Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click p.StartInfo.UseShellExecute = False p.StartInfo.FileName = "cmd" p.StartInfo.Arguments = "/c ping 127.0.0.1 >C:\""Local Host.txt""" p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden p.Start() p.WaitForExit() Dim s As String = IO.File.ReadAllText("C:\Local Host.txt") s = "Ping Local Host " & Now.ToShortDateString & vbCrLf & s IO.File.WriteAllText("C:\Local Host.txt", s) End Sub 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...
-
Jan 5th, 2013, 02:03 PM
#8
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!
-
Jan 5th, 2013, 06:30 PM
#9
Thread Starter
Member
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.
-
Jan 6th, 2013, 12:36 PM
#10
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!
-
Jan 6th, 2013, 09:05 PM
#11
Re: Append text to begining of text file
 Originally Posted by TheThinker
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.
-
Jan 7th, 2013, 09:39 AM
#12
Thread Starter
Member
Re: Append text to begining of text file
 Originally Posted by Edgemeal
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|