Solved

search text file

Posted on 2001-06-21
3
120 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
ID: 6214656
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
ID: 6214683
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
ID: 6214688
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 90
Recommendation vb6 to vb.net or others 14 179
VB6 - Scroll Mouse wheel on Picturebox 13 79
VB script to continue despite error 2 17
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

808 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