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

VBScript - Delete Multiline variable from text file

Status
Not open for further replies.

ToToRoYN

MIS
May 10, 2018
2
0
0
US
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.
 
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.
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top