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

Deleting lines.

Status
Not open for further replies.

Phreeneazie

Technical User
Apr 6, 2003
27
GB
Hi,

some of you may have seen my name pop up occasionally. For those that have, "Hi"! For those that haven't, "Hi, you'll soon realise I don't know the slightest thing about VB :-(".

I have a requirement at work to "edit" a text file and delete some lines of data. These lines are usually 3 from the top of file and 1 from the bottom. They ALWAYS start with a ~ (tilde) character. I know the exact wording if needed, but is there a way to delete the lines containing the ~ ? What I would ideally like to do, is this:

Open the file;
Look for a ~;
If found, delete that line completely and "move" the rest of the data up one line.
Once complete, overwrite the existing file.

Can anyone help me on this? I would (as always) be extremely grateful! As I said right at the top, I have no great knowledge of VB (I keep on asking my boss to send me on a course, but it's the same old, "Hmmm, we'll look at it with next years budget" :rolls eyes:).

Thanks to all!
 
Something like this ?
Code:
Const ForReading=1,ForWriting=2
Const myFile="\path\to\file.txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile(myFile,ForReading)
a=Split(f.ReadAll,vbCrLf)
f.Close
Set f=fso.OpenTextFile(myFile,ForWriting,True)
For i=0 To UBound(a)
  If Left(a(i),1)<>"~" Then f.WriteLine a(i)
Next
f.Close

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

thank-you! That is doing *exactly* what I want it to do :) One question - how can I make it work so that it opens every file in a specified directory? I usually have 9 files that I need to do and would like to be able to get the script to open each one in turn (and order) rather than have me specify the file in the script.

Thanks again, that's brilliant!

Neil.
 
Just play with the GetFolder method of the FileSystemObject object and with the Files collection.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top