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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete the top and bottom line in a text file 1

Status
Not open for further replies.

Gordicron

Technical User
May 10, 2007
2
GB
Hi,

I'm quite new to VBScript and have been creating scripts to clean up some lists of URLs that I have. One type of list I have is generated from emails and is a list of very similar URLs. There is no fixed number of URLs that may be in the list but I always need to remove the first and last URLs as they are useless and waste time.

So I may have a list like this:


The first and last URLs are never the same, so I can't just filter them out by the URL itself. The only constant for these is that it is always the first and last entries in the list that I want to remove.

My question therefore is this:

Is it possible to create a VBScript that can remove the top and bottom lines in a text file, no matter the number of lines in the text file? If so, how?

I have looked at the objTextFile.SkipLine command, however all the examples I have seen have assumed that the text file is of a fixed number of lines (which mine aren't). So I can't just type out skip, read, read, skip, etc...

There must be a way to read all the text lines into an array and then say skip line 1, read all the others and then skip the last line (whatever number that line will be).

Like I say, I'm new to this - so apologies if this is a silly question! :)

Would really appreciate some help with this one.

Regards

- Gord
 
A starting point:
a = Split(objInputFile.ReadAll,vbCrLf)
For i = 1 To UBound(a) - 1
objOutputFile.Writeline a(i)
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
maybe something like this...

Code:
Const ForReading = 1
Const ForWriting = 2

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\temp\somefile.txt", ForReading)
Dim arrTemp : arrTemp = Split(objFile.ReadAll, VbCrLf)
objFile.Close
Set objFile = objFSO.OpenTextFile("c:\temp\somefile.txt", ForWriting)
Dim i
For i = 1 To UBound(arrTemp) - 1
	objFile.WriteLine arrTemp(i)
Next
objfile.Close

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Try this
Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)


strLine = objFile.ReadLine
strLine = objFile.ReadLine
Do Until objFile.AtEndOfStream
    strContents = strContents & strLine & vbCrLf
    strLine = objFile.ReadLine
    
    
   
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.Write strContents

objFile.Close
 
Thanks for the replies; especially to pwise - that code works perfectly! :)

Just a quick question - whereabouts in the code are the lines to skip actually defined? I'm just wondering for future reference. So say, I decided I wanted to skip the first two lines instead and keep the rest of the text... what would I change in the code to do achieve that? Just trying to work out whereabouts in the code that actually happens.

Thanks again
- Gord
 
pwise's method of skipping the line is his initial

strLine = objFile.ReadLine

The method PHV showed would let you adjust what lines to begin with and how many lines before the end to stop.

i = 1 would mean start reading at the second line
To stop at a certain line before the end you would play with the UBound(xxx) - 1

If your initial file had a blank line at the end you might have to change it to UBound(xxx) - 2



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top