SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Member
    Join Date
    Sep 2009
    Posts
    262
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Search for a word in a text file

    Hi
    I have the following code. It basically opens a text file and read it line by line. I want to:
    1. Search for a specific word in the line
    2. Return the line number that the word was found in. (the searched word is very unique and it found only once in the text file.)
    Here is the code:
    Option Explicit
    Dim oFSO, sFile, oFile, sText, mText
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "C:\My_log.log"
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
    sText = oFile.Readline
    If sText = "ortismi.exe" Then
    msgbox "Found"
    End If
    Loop

  2. #2
    Member
    Join Date
    May 2010
    Posts
    164
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    <font class="small">Code:</font><hr /><pre> Option Explicit
    Dim oFSO, sFile, oFile, sText, mText, linenum
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "C:\My_log.log"
    linenum = 0
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
    sText = oFile.Readline
    linenum = linenum + 1
    If StrComp(sText, "ortismi.exe") &gt;= 0 Then
    msgbox "Found in line " &amp; linenum
    End If
    Loop </pre><hr />

  3. #3
    Moderator
    Join Date
    Jul 2005
    Location
    Delhi
    Posts
    15,575
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    7 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    Use ReadAll of FSO and then use InStr to check for file presence

  4. #4
    Member
    Join Date
    Sep 2009
    Posts
    262
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    Thank you,
    Is there a way to make the script move to the next line? This is my situation: once ortismi.exe is found, I need to go to the 6 lines below and parse them. The 6 lines below ortismi.exe are the following:
    Memory usage: 72,672 K
    GDI objects: 200
    USER objects: 193
    Thread count: 15
    Handle count: 478
    Maximum working set: 1380

    So I need to parse them ( I know that I need to use Split function to do this task) but I want it move to the next line

  5. #5
    Moderator
    Join Date
    Jul 2005
    Location
    Delhi
    Posts
    15,575
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    7 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    InStr will give you the index of the occurrence of this exe and then you use Mid to cut the text from that position and later use InStr to get the index vbNewLine. Do it 6 times to get the 6 the index and then use Left to keep the text till the index you found.

  6. #6
    Member
    Join Date
    Sep 2009
    Posts
    262
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    No Tarun,

    Let’s say that the search word ortismi.exe is found in line 710. Now, I want to go to the next line 711 which has Memory usage: 72,672 K and get 72,672 where I can export it to excel file. I can’t get it to move to the next line. Here is my code again:
    Option Explicit
    Dim oFSO
    Dim sFile
    Dim oFile
    Dim sText
    Dim counter
    Dim sCounter
    Dim nText
    Dim eCounter
    Dim i


    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "C:\log.txt"
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
    sText = oFile.Readline
    counter = counter +1
    If InStr (sText , "ortismi.exe")&gt;0 Then
    ‘Add 6 to the counter where ortismi is found (i.e., 710)
    eCounter = Counter +6
    ‘eCounter is suppose to be 716
    For i = Counter to eCounter
    split sText, ":"
    Next
    End If
    Loop

  7. #7
    Member
    Join Date
    May 2010
    Posts
    164
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    Put another readline inside of your second loop

  8. #8
    Member
    Join Date
    Sep 2009
    Posts
    262
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    I did try that, but it keeps reading ortism.exe 6 times.

  9. #9
    Member
    Join Date
    Aug 2008
    Posts
    150
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    Just read the next 6 lines once you find the file name you are looking for.

    <font class="small">Code:</font><hr /><pre>
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "C:\log.txt"
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
    sText = oFile.Readline
    If InStr (sText , "ortismi.exe")&gt;0 Then
    For iterator = 1 to 6 step 1
    sText2 = oFile.Readline
    msgbox(sText2)
    Next
    End If
    Loop
    </pre><hr />

  10. #10
    Member
    Join Date
    Aug 2008
    Posts
    150
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Search for a word in a text file

    I tried the above on a text file and it worked for me, reading the next 6 lines.

    [ QUOTE ]
    I did try that, but it keeps reading ortism.exe 6 times.

    [/ QUOTE ]

 

 
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 04:15 PM.

Copyright BetaSoft Inc.