Hi! Ich have a very complicated question, and im a total beginner in VBS... I have a .txt File, and I need to search for a string... When he found it, he should write the line and the following to a file. After that he should do the same again, because the files containes the string 2 times. But there is another problem... The String to search for shoud be readed from another .txt files called parameters. In parameters.txt the string is in the second line. I know its a bit complicated, but i hope you can help me, because I do have to do this for work and not for my fun...
I know... The point is that im way to stupid to code it alone xD And I do not have enough time to change every line 10 times before it works... You are a pro and you can code it in around 10 min... So move to pity and help me...
My Problem is that I cant define sText like this... Without Writing the code works, it returns the number of found strings, and shows the entire line. How to wite this Line and the 2 Following ones?
if you change your code to find/display the lines slightly (and just once (!)):
you can add "output to file" in a systematic way:
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) Dim sFSpec : sFSpec = ".\sirect.txt" Dim sAll : sAll = oFS.OpenTextFile( sFSpec ).ReadAll()
sFSpec = ".\sirect_out.txt" Dim oTS : Set oTS = oFS.CreateTextFile( sFSpec, True )
Dim oRE : Set oRE = New RegExp oRE.Global = True oRE.Multiline = True ' ^ and $ will match at E/BOL oRE.Pattern = "^(.*rofl.*)|(.*telefon.*)$"
Dim oMTS : Set oMTS = oRE.Execute( sAll ) Dim oMT For Each oMT In oMTS Dim sText : sText = oMT.Value WScript.Echo sText oTS.WriteLine sText Next oTS.Close
What I'm not sure about is how many matches you expect in the file. Two? Three? More than two? Depending on your decision you may consider an indexed loop:
Dim nIdx For iIdx = 0 To <?> ' ? <== 1 | 2 | oMTS.Count - 1 Dim oMT : Set oMT = oMTS( iIdx )
Good luck!
ehvbs
BTW: impressive work for 'a total beginner in VBS' who is too 'stupid to code it alone '!
Dim oMTS : Set oMTS = oRE.Execute( sAll ) If 2 <> oMTS.Count Then WScript.Echo "no two matches" Else WScript.Echo oMTS( 0 ).Value oTS.WriteLine oMTS( 0 ).Value WScript.Echo oMTS( 1 ).Value oTS.WriteLine oMTS( 1 ).Value End If oTS.Close ...
Hi! Thank you for your help... Now, it always says "no two matches" but the file containes "rofl" 2 times... Hmmm... I think if I got this and he writes the following 2 lines im finished... So again : Thank you and im hoping that I can stop spamming around here ;)
I need to read parameters from a file. (para.txt) 1. Line is a mail-adress for later processing 2. Is the value to search for (Like Rofl / telefon) Would you be so kind as to help me solving this problem, too? xD
you are welcome. As to your second problem: I'm confident that you can do this on your own. You know how to open a text file for reading and after looking at the sample code for .ReadLine in the VBScript Docs you'll have no problems with using two .ReadLine()s to get the email address and the search string into variables.
Give it a try and post here in case of difficulties.
Ich lese es jetzt erst : From: Germany Das hättest du ja mal früher sagen können! Und ich müh mich hier mit englisch ab ;) Zur Belohnung habe ich übrigens erstmal alle Banner angeklickt die mir hier untergekommen sind =)
< Message edited by Sirect -- 6/15/2008 10:18:34 PM >