Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting lines in a text file 1

Status
Not open for further replies.

miteshlad2003

Technical User
Oct 25, 2003
42
0
0
GB
Hello All,

I have a simple text file and and I wish to remove the first 3 lines and the last 5 lines. How can I do this?

I am really really stuck!

Cheers

Miteshlad2003
 
Just to give you what I am trying to achieve here...

I am trying to ping a server 30 times and place the information into a text file. If there are more than 25 Request time outs in the file then I get a prompt on my screen.

Hope this helps!

Miteshlad2003
 
this is how i did it:

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

   Set Readfile = FileObject.OpenTextFile("yourfile.txt",ForReading,,-2)
    ThisTxt = Split(ReadFile.ReadAll,vbCrLF)
    
    i=0
    For Each Line In ThisTxt
        i=i+1
        if i < 4 or i > 25 then
             druckdatei = druckdatei & Line & vbcrlf
       end if
    Next
    Readfile.Close


druckdatei will contain your wanted lines only
 
of course, I was writing to rashed.

it would rather be: if i > 3 or i < 26 then
to get your results, and if you want to write the result to a file you need to do that about this way:

Code:
FileObject.CreateTextFile("c:\Temp\yourfile.txt")
	Set FileName = FileObject.GetFile("c:\Temp\yourfile.txt")

	Set WriteFile = FileName.OpenAsTextStream(ForWriting,True)
	WriteFile.WriteLine(druckdatei)
	WriteFile.Close

hope you can figure out the rest by yourself.

Dav
 
If you are getting your text file by redirecting the ping command to a file, you could also work on cutting the file down to begin with.

ping <addr> | find "Packets: Sent" > myfile.txt

will give you just the result line. You can then read the value for lost packets.

ping <addr> | find "timed out" > myfile.txt

will give you just the timed out lines and then count them.
 
wcburton,

I find your solution the easiest, however when I try to place the information in my script i get an error message. Below is part of my script:

Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /K ping socbackup1 | find "Lost" >c:\servers\server1.txt"
Set oShell = Nothing

Also it keeps on opening up a new dos windows and won't close!!!!!

Any ideas?

Thanks

Miteshlad2003
 
The CMD window won't close because you are using the /K switch. Change it to /C.
 
I am trying but i am getting this error each time:

(5, 44) Microsoft VBScript compilation error:
Expected end of statement

Thanks
 
I've now tried doing it this way and now the textfile isn't being created!!!!!:


option explicit

Dim oShell
Set oShell = WScript.CreateObject ("WScript.shell")
oShell.run "cmd /c ping - n 10 socbackup1 | find & Chr(34) & Lost & Chr(34) & > c:\serverdumps\soc.txt"
Set oShell = Nothing

thanks

Miteshlad2003





 
Try this:
oShell.Run "cmd /c ping - n 10 socbackup1 | find " & Chr(34) & "Lost" & Chr(34) & " > c:\serverdumps\soc.txt"


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I discover another problem:
oShell.Run "cmd /c ping [highlight]-n[/highlight] 10 socbackup1 | find " & Chr(34) & "Lost" & Chr(34) & " > c:\serverdumps\soc.txt"


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
How can I resolve the problem with the -n ???

It needs to be in there as it needs to ping 30 times...

Thanks!

Miteshlad2003
 
PHV,

ignore last email......yes well spotted!!!!

Thanks

Miteshlad2003
 
Try this:
oShell.Run "cmd /c ping -n 10 socbackup1 | find " & Chr(34) & "Lost" & Chr(34) & " > c:\serverdumps\soc.txt", 1, True

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hello all,

[1] Use preferrably exec method without invoking cmd at all.
[2] The following shows you the "|" bad parameters.
[3] Work from the .stdout.readall. It is much more manageable.
Code:
set wshshell=createobject("wscript.shell")

'This line the readall will show you the "bad parameter |"
'set oExec=wshshell.exec("ping -n 10 [URL unfurl="true"]www.google.com[/URL] | find " & chr(34) & "Lost" & chr(34) & " > c:\serverdumps\soc.txt")

'testing line
set oExec=wshshell.exec("ping -n 10 [URL unfurl="true"]www.google.com")[/URL]

do while oExec.status=0
	wscript.sleep 50
loop
s=oExec.stdout.readall
wscript.echo s
set oExec=nothing : set wshshell=nothing
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top