mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Read speciffic line in text file

Author Message
simonc1970

  • Total Posts : 26
  • Scores: 0
  • Reward points : 0
  • Joined: 5/7/2007
  • Status: offline
Read speciffic line in text file Wednesday, July 28, 2010 6:29 AM (permalink)
0
 
Hello, I am searching a text file for a string, when I find it I want to throw a message box with the entire line.  I can do this but the problem is the script finds the search text and displays the next line down.  I know which line the search text is on by using objFile.Line - 1   How do I use readline to send a message box with that speciffic line?  I would think msgbox objFile.ReadLine(48)  but this does not work, any help would be greatly appreciated!!!
 
Const ForReading=1
 
strSearch = "PC123"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strSearch
Set objFile = objFSO.OpenTextFile("c:\PC_DATA.txt", ForReading)
Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString) 
    If  colMatches.Count > 0 Then       
        strLine2 = objFile.Line - 1        
        msgbox objFile.ReadLine
        objFile.Close 
        Exit do       
    End If   
Loop
objFile.Close
#1
    rasimmer

    • Total Posts : 2360
    • Scores: 163
    • Reward points : 0
    • Joined: 3/19/2009
    • Location: Cedar Rapids, IA
    • Status: offline
    Re:Read speciffic line in text file Wednesday, July 28, 2010 8:36 AM (permalink)
    0
    Try this:

    If  colMatches.Count > 0 Then        
        For Each objMatch in colMatches
            WScript.Echo "Pattern Match: " & objMatch.Value 
        Next
    End If   
    #2
      simonc1970

      • Total Posts : 26
      • Scores: 0
      • Reward points : 0
      • Joined: 5/7/2007
      • Status: offline
      Re:Read speciffic line in text file Wednesday, July 28, 2010 10:10 AM (permalink)
      0
      Thanks for the reply rasimmer, unfortunately that only brings back the matching value and I need to display the entire line wher the string matches.  so in a file setup as follows
       
      1
      2
      33PC12344
      5
      6
       
      the objMatch.Value  does bring back the PC123  but I need it to bring back the entire line 33PC12344  any suggestions?  Thanks again
      #3
        rasimmer

        • Total Posts : 2360
        • Scores: 163
        • Reward points : 0
        • Joined: 3/19/2009
        • Location: Cedar Rapids, IA
        • Status: offline
        Re:Read speciffic line in text file Thursday, July 29, 2010 1:55 AM (permalink)
        0
        I am no RegEx expert, but try:

        objRegEx.Pattern = "/s*" & strSearch
        #4
          simonc1970

          • Total Posts : 26
          • Scores: 0
          • Reward points : 0
          • Joined: 5/7/2007
          • Status: offline
          Re:Read speciffic line in text file Friday, July 30, 2010 6:46 AM (permalink)
          0
          here is the solution, you need to open the file first, find the line with the matching text, then re-open it and use a counter to stop before the desired line, then read it.  MS could have made this so much easier!
           
          Const ForReading = 1
          strSearch = "PC123"
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          Set objRegEx = CreateObject("VBScript.RegExp")
          objRegEx.Pattern = strSearch
          Set objFile = objFSO.OpenTextFile("c:\PC_DATA.txt", ForReading)
          Do Until objFile.AtEndOfStream
              strSearchString = objFile.ReadLine
              Set colMatches = objRegEx.Execute(strSearchString) 
             
              If  colMatches.Count > 0 Then 
               strLine2 = objFile.Line - 2      
               objFile.Close     
            Set objFile = objFSO.OpenTextFile("c:\PC_DATA.txt", ForReading)
            For i = 1 to strLine2
                objFile.ReadLine
            Next
            strLine3 = objFile.ReadLine
            MsgBox strLine3
            objFile.Close
            Exit do     
              End If   
          Loop
          objFile.Close
           
          #5
            rasimmer

            • Total Posts : 2360
            • Scores: 163
            • Reward points : 0
            • Joined: 3/19/2009
            • Location: Cedar Rapids, IA
            • Status: offline
            Re:Read speciffic line in text file Friday, July 30, 2010 7:53 AM (permalink)
            0
            You're making it WAY to complicated.  The regex pattern can be modified to display the line, but you could also do it this way:

            Const ForReading = 1
            strSearch = "PC123"
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFile = objFSO.OpenTextFile("c:\PC_DATA.txt", ForReading)
            Do Until objFile.AtEndOfStream
                strSearchString = objFile.ReadLine 
                If Instr(UCase(strSearchString), UCase(strSearch)) Then WScript.Echo strSearchString
            Loop
            objFile.Close
            #6
              simonc1970

              • Total Posts : 26
              • Scores: 0
              • Reward points : 0
              • Joined: 5/7/2007
              • Status: offline
              Re:Read speciffic line in text file Wednesday, September 08, 2010 7:35 AM (permalink)
              0
              You are right, I was making it way too hard, that is soo much easier.  Thanks!
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • 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
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.8
                mbt shoes www.wileywilson.com