Solved

VBScript code to delete any data between two characters

Posted on 2009-05-14
4
589 Views
Last Modified: 2012-05-07
I'm completely stumped on how to do this.

I have a file full of html code. I need to search through it and remove some code. The problem I have is that there is variable between [ and ], and I am not sure how to go about this.

I'm use to using replace and variables etc at a basic level, but this is well beyond me!

example code:
Stuff I need to keep<br /><font color="#ff0000">[variable data]</font><br /><font color="#000000">More stuff here I need to keep


I need a script/function to search and delete, leaving me with:
Stuff I need to keep<br /><br /><font color="#000000">More stuff here I need to keep

The square brackets don't appear anywhere else in the file, so they can be used as an anchor point for the deletion.
0
Comment
Question by:Burns2007
  • 2
4 Comments
 
LVL 7

Expert Comment

by:BarryTice
ID: 24394601
First, read your text file into a single string variable, say, strText.
Then, try something like this:
Do While InStr(strText, "[") > 0
  intLeft = InStr(strText, "[")
  strLeft = Left(strText, intLeft - 23)
  intRight = InStr(strText, "]")
  strRight = Right(strText, Len(strText) - strRight - 7)
  strText = strLeft & strRight
Loop
 
This could be optimized somewhat for speed and memory usage, but this is nice and clear.

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24395575
Hi,

Try this as well.

Pass the text into strText and it'll remove all the of [text] portions.
While InStr(strText,"[") > 0
	intOpen = InStr(strText,"["):intClose = InStr(strText,"]")
	strText = Replace(strText,Mid(strText,intOpen,(intClose-intOpen)+1),"")
Wend

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24395598
Barry, i couldn't personally get yours to work. What are the 23 and 7 for?

Not saying it's wrong but I like to get my head round other methods.
0
 
LVL 1

Accepted Solution

by:
maytan80 earned 485 total points
ID: 24397349
Use the split method. It will create an array on the variable

so x<y>z

I would do a split on "<" character giving me a 2 record array x and y>z
then do a split on > giving me x in the first array and y and z in the second array

Now mix and match

character array m = split(str, character/delimiter)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question