Solved

search text file

Posted on 2001-06-21
3
117 Views
Last Modified: 2010-05-02
I'm looking for a simple way to open a text file and search for any 8 characters and ".exe"

Then it should display whatever the 8 characters are.



0
Comment
Question by:jlymn
  • 2
3 Comments
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Option Explicit

Private Sub Form_Load()
Dim lin As Long, col As Long

If CheckFile("c:\test\test.txt", "andy", lin, col) Then
    Debug.Print "line = ", lin, "col = ", col
End If


End Sub


Public Function CheckFile(sFilePath As String, ByVal sSearch As String, lin As Long, col As Long) As Boolean

    Dim i As Long, sline
    i = FreeFile
    lin = 0
   
    sSearch = sSearch & ".exe"
    Open sFilePath For Input As i
    While Not EOF(i)
        Line Input #i, sline
        lin = lin + 1
        If InStr(sline, sSearch) Then
            col = InStr(sline, sSearch)
            Close i
            CheckFile = True
            Exit Function
        End If
    Wend
   
    Close i
   


End Function
0
 
LVL 3

Expert Comment

by:cmgarnett
Comment Utility
Sub OpenTextFileTest
    Const ForReading = 1
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile("c:\testfile.txt", ForReading,TristateFalse)
    strWork = f.ReadAll
    f.Close

    intStart = InStr(strWork,".exe")
    strName = Mid$(strWork, intStart - 8,8)
End Sub

Hope this starts you off.
0
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
Comment Utility
Sorry, I think this is what you meant

Option Explicit

Private Sub Form_Load()
Dim lin As Long, col As Long, sdata As String

If CheckFile("c:\test\test.txt", ".exe", lin, col, sdata) Then
    Debug.Print sdata
End If


End Sub


Public Function CheckFile(sFilePath As String, ByVal sSearch As String, lin As Long, col As Long, sdata As String) As Boolean

    Dim i As Long, sline
    i = FreeFile
    lin = 0
   
   
    Open sFilePath For Input As i
    While Not EOF(i)
        Line Input #i, sline
        lin = lin + 1
        If InStr(sline, sSearch) > 8 Then
            col = InStr(sline, sSearch)
            sdata = Mid(sline, col - 8, 8)
            Close i
            CheckFile = True
            Exit Function
        End If
    Wend
   
    Close i
   


End Function
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now