Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

search text file

Posted on 2001-06-21
3
Medium Priority
?
125 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 200 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

927 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