Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Read things in File

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61331
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read things in File
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Read things in File - 6/12/2008 11:20:14 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
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...

THX

Sirect
 
 
Post #: 1
 
 RE: Read things in File - 6/13/2008 12:18:32 AM   
  dm_4ever


Posts: 2666
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Search for

FileSystemObject
OpenTextFile
InStr Function
Regular Expression

There have been countless questions similar to this already.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sirect)
 
 
Post #: 2
 
 RE: Read things in File - 6/13/2008 3:50:28 AM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
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...

Thank you

Sirect

(in reply to Sirect)
 
 
Post #: 3
 
 RE: Read things in File - 6/13/2008 5:07:27 AM   
  dm_4ever


Posts: 2666
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
We are here to help....not write code for you.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sirect)
 
 
Post #: 4
 
 RE: Read things in File - 6/13/2008 6:18:19 AM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline

      

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?

MFG

Sirect

(in reply to Sirect)
 
 
Post #: 5
 
 RE: Read things in File - 6/13/2008 7:05:50 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

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 '!

(in reply to Sirect)
 
 
Post #: 6
 
 RE: Read things in File - 6/13/2008 7:57:25 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
Hi!
Im expecting the string 2 times, because the process will start and end...
I know its complicated, but noone cares xD

btw:
Thank you ehvbs!
You saved my ass ;)
But how to let him write the following 2 lines when finding something?

Sirect

< Message edited by Sirect -- 6/13/2008 8:02:00 PM >

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Read things in File - 6/14/2008 3:05:03 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

change the RegExp's pattern and adapt the output code to "exactly 2 matches":

...
oRE.Pattern   = "^((.*rofl.*)|(.*telefon.*))$\n^.*$\n^.*$"

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
...

Good luck!

ehvbs

(in reply to Sirect)
 
 
Post #: 8
 
 RE: Read things in File - 6/14/2008 4:34:02 AM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
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 ;)

Sirect

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: Read things in File - 6/14/2008 5:07:34 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

my test file:


      

my output:


      

Double check the RegExp's pattern and/or post a sample of your input data.

Thanks

ehvbs

(in reply to Sirect)
 
 
Post #: 10
 
 RE: Read things in File - 6/14/2008 6:43:48 AM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
May you could post the whole code again?
I think I made a mistake pasting and changing it ;)

(in reply to Sirect)
 
 
Post #: 11
 
 RE: Read things in File - 6/14/2008 7:43:10 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline

      

(in reply to Sirect)
 
 
Post #: 12
 
 RE: Read things in File - 6/14/2008 8:40:07 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
Man you are the best!!
THANKS!

Now my second problem:

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

Sirect

(in reply to Sirect)
 
 
Post #: 13
 
 RE: Read things in File - 6/15/2008 5:35:25 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

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.

Good luck!

ehvbs

(in reply to Sirect)
 
 
Post #: 14
 
 RE: Read things in File - 6/15/2008 6:18:29 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
Hi!
I really like people like you in forums, because you help me with my script, but you help me to learn for myself, too ;)
Thats Great!

1 Question Remaining ;)


      

Sending the mail is following, but I cant post this part here...

My Question is:
He searches for "sText" now, but he should search for the value of sText...
How to do that?

Sirect

btw :
I think i can stop penetrating you with questions, after this is working ;)

(in reply to ehvbs)
 
 
Post #: 15
 
 RE: Read things in File - 6/15/2008 9:16:09 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

to  'splice' the value of sText into oRE.Pattern, use

  oRE.Pattern   = "^((.*" & sText & ".*)|(.*" & sText & ".*))$\n^.*$\n^.*$"

or - because you don't look for two search patterns (rofl, telefon) anymore

  oRE.Pattern   = "^.*" & sText & ".*$\n^.*$\n^.*$"

Not tested, so I may have messed it up -  Good luck to both of us!

ehvbs

(in reply to Sirect)
 
 
Post #: 16
 
 RE: Read things in File - 6/15/2008 10:17:18 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
You are god.

...


Thanks!!!

Sirect


btw:

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 >

(in reply to ehvbs)
 
 
Post #: 17
 
 RE: Read things in File - 6/16/2008 8:45:26 PM   
  Sirect

 

Posts: 25
Score: 0
Joined: 6/12/2008
Status: offline
@ehvbs

Got another Question:

Could you change the source to use OpenAsTextStream?
This is working:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set f = objFSO.GetFile("\\XXXXXXXX\123.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
mesg = ts.ReadLine
WScript.echo mesg

But I cant use it in my Script, because he says "Object Required" all the Time :-(
I didnt got that much time left, may you could help me?

Thanks!

BTW:

Dim oMTS   : Set oMTS   = oRE.Execute( sAll )

Is not working with Stream? Type Mismatch : Execute

< Message edited by Sirect -- 6/16/2008 8:52:58 PM >

(in reply to Sirect)
 
 
Post #: 18
 
 RE: Read things in File - 6/16/2008 8:58:39 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Sirect,

please identify the line and the error message exactly.

Thanks

ehvbs

(in reply to Sirect)
 
 
Post #: 19
 
 RE: Read things in File - 6/16/2008 9:01:27 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Straight from the VBScript Docs:

Execute Method 
Executes a regular expression search against a specified string.

(in reply to ehvbs)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read things in File Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts