×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VBScript - Delete Multiline variable from text file

VBScript - Delete Multiline variable from text file

VBScript - Delete Multiline variable from text file

(OP)
I can usually figure most things out on my own but this here issue has had me stumped for quite some time.

CODE

Apple
  Fruit
  Sweet
Onion
  Vegetable
  Savory 

Given the above text, I have no problem removing the lines of text pertaining to Apple. I would use something like this:

CODE

Const ForReading = 1
Const ForWriting = 2

Dim objFSO, objFile
Dim strFile, strTxt1, strTxt2, strTxt3, strAll, strTxtN

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "C:\TEMP\TNS_Test\TNSNAMES.ORA"
strTxt1 = "Apple"
strTxt2 = "  Fruit"
strTxt3 = "  Sweet"

Set objFile = objFSO.OpenTextFile(strFile, ForReading)
Do Until objFile.AtEndOfStream
	strAll = objFile.ReadLine
	If (inStr(strAll, strTxt1) = 0 _
	AND inStr(strAll, strTxt2) = 0 _
	AND inStr(strAll, strTxt3) = 0) Then
		strTxtN = strTxtN & strAll & vbCrLf
	End If
Loop
objFile.Close

Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.Write strTxtN
objFile.Close 

However, in the instances I actually want to use this code the data is not 100 unique. My file would look like this:

CODE

Apple
  Fruit
  Sweet
Onion
  Vegetable
  Savory
Banana
  Fruit
  Sweet
Tomato
  Fruit
  Savory 

If I ran the code above, my out put would be this:

CODE

Onion
  Vegetable
  Savory
Banana
Tomato
  Savory 

How could I remove only Apple and its attributes? I tried setting the variable to:

CODE

strTxt = "Apple" & vbCrLf & _
         "  Fruit" & vbCrLf & _
         "  Sweet" 

Using WScript.Echo outputs the variable perfectly but it will not work in the code above. I tried using Replace but then I get a text file with blank lines. I can't figure out how to delete the blank lines.

Any ideas would be appreciated.

RE: VBScript - Delete Multiline variable from text file

(OP)
I JUST FIGURED IT OUT!! This is not the first time I've done this. I think having to distill my issue down to a non-specific example helps me clarify my actual problem and come up with an answer. This is what I've come up with:

CODE

Const ForReading = 1
Const ForWriting = 2

Dim objFSO, objFile
Dim strFile, strTxt, strAll, strTxtN

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Text.txt"
strTxt = "Apple" & vbCrLf & _
		 "  Fruit" & vbCrLf & _
		 "  Sweet"

Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strAll = objFile.ReadAll
objFile.Close

strTxtN = Replace(strAll, strTxt & vbCrLf, "")

Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.Write strTxtN
objFile.Close 

So, yeah. There it is. I hope this helps someone out there.

RE: VBScript - Delete Multiline variable from text file

Hi,

The way I’d approch it, assuming that whenever you have Apple starting in postion 1 of that line, every line below is deleted that has SPACE in the first position.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close