find and replace text using reg expressions

July 6, 2010 at 09:27:19
Specs: Windows XP
I am new and need some help/advice please.

I have a folder with one .txt file in it and that's it. I need to write a .bat file that can do the following:
- open a .txt file regardless of its name since it always changes. If the file name has to be hard coded in that's ok too.
- find and remove all the html tags in the text file.
- this is the regular expression i've found that can remove all html tags: <(.|\n)+?>

can anyone please help me do this or at least point me in the right direction?


See More: find and replace text using reg expressions

Report •

#1
July 6, 2010 at 13:39:32
Well, it's not a perfect solution, but that regular expression should do the job.

Batch, however, will not. VBScript, on the other hand. . . (Save as a .vbs, then drag the .txt file over the .vbs file.)

Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp
regEx.Pattern = "<(.|\n)+?>"
regEx.Global = True
fso.OpenTextFile(WScript.Arguments(0) & ".out", 2, True).Write _
  regEx.Replace(fso.OpenTextFile(WScript.Arguments(0)).ReadAll, "")


Report •

#2
July 6, 2010 at 14:21:52
Thanks! It works perfect!

If my original file name is: text_file.txt then it resaves it as text_file.txt.out. Is there a way to just overwrite the file or append "_1" to the file name so i don't have to manually remove the .out to turn it back into a txt file?

Thanks again for your help.


Report •

#3
July 6, 2010 at 14:40:48
Sure.
Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = New RegExp
regEx.Pattern = "<(.|\n)+?>"
regEx.Global = True
txtFile = regEx.Replace(fso.OpenTextFile(WScript.Arguments(0)).ReadAll, "")
fso.OpenTextFile(WScript.Arguments(0), 2, True).Write txtFile


Report •

Related Solutions

#4
July 6, 2010 at 14:44:14
It overwrote the file perfectly! thanks again for your help...

P.S. do you know of any good websites to try and start learning this stuff? Just so i can learn to write small files like the one you just did for me.


Report •

#5
July 6, 2010 at 14:57:18
Unfortunately, I do not know of a good beginner's resource, and a quick Google search shows most VBScript tutorials are aimed at ASP or client-side web page scripting.

I use MSDN. While it's a great reference site, a beginner would probably be lost.

What you really need is a good VBScript book for Windows administrators.


Report •

#6
July 6, 2010 at 20:30:04
I've found this downloadable to be very "help"ful (ha), although it's not a "manual", it helps with syntax and offers object-interface info:
http://www.microsoft.com/downloads/...
(for scripting helpfile download, script56.chm)

Report •

Ask Question