Results 1 to 11 of 11

Thread: Search Text File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    n/a
    Posts
    89

    Search Text File

    Anyone know the best way to search through a text file for a specific string?
    n/a

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Load the entire file into a String variable and use InStr()
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    here is a little parser that i jsut threw together so that i could change stuff on around 150 web pages for my site relaunch ...
    You can modify as required
    Regards
    Stuart
    VB Code:
    1. '2 command buttons, 1 label and 1 filelist box
    2. Dim MyPath As String
    3. Dim FileLoop As Long
    4. Dim MyString As String
    5. Dim MyFile As String
    6. Dim MyLen As Long
    7. Dim MyFind As String
    8. Dim MyReplace As String
    9. Dim MyCopy As String
    10. Dim MyLenCheck As Double
    11. Dim MyCounter As Long
    12.  
    13. Private Sub Form_Load()
    14.     MyPath = "C:\My Documents\MyWeb"
    15.    
    16.     File1.Path = MyPath
    17.     File1.Pattern = "*.htm"
    18.     FileLoop = File1.ListCount
    19.     MyCounter = 0
    20.     File1.ListIndex = MyCounter
    21. End Sub
    22.  
    23. Private Sub Command1_Click()
    24.     File1.ListIndex = MyCounter
    25.    
    26.     MyFile = MyPath & "\" & File1.List(MyCounter)
    27.     MyLen = FileLen(MyFile)
    28.    
    29.     Open MyFile For Input As #1
    30.     MyString = Input(MyLen, #1)
    31.     Close #1
    32.    
    33.     MyFind = "Whatever"
    34.     MyReplace = "Whats It"
    35.    
    36.     MyCopy = Replace(MyString, MyFind, MyReplace)
    37.     MyLenCheck = (Len(MyCopy) - Len(MyString)) / (Len(MyReplace) - Len(MyFind))
    38.     Label1 = MyLenCheck
    39.    
    40.     Command2.SetFocus
    41. End Sub
    42.  
    43. Private Sub Command2_Click()
    44.     Open MyFile For Output As #1
    45.     Print #1, MyCopy
    46.     Close #1
    47.    
    48.     MyCounter = MyCounter + 1
    49.     Command1.SetFocus
    50.     If MyCounter >= FileLoop Then Command2.Enabled = False
    51. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    n/a
    Posts
    89
    Thanks guys, you are the greatest.
    n/a

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Another (aka better ) approach :

    VB Code:
    1. Private Sub Form_Load()
    2.     MsgBox "Is " & Chr(34) & "SET" & Chr(34) & " in c:\autoexec.bat : " & isStringInFile("SET", "c:\autoexec.bat")
    3. End Sub
    4.  
    5. Private Function isStringInFile(strString As String, strFile As String) As Boolean
    6.     Open strFile For Input As #1
    7.         isStringInFile = InStr(Input(LOF(1), 1), strString) <> 0
    8.     Close #1
    9. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Ahhhh BOLLOX!! geesh i threw that together in 5 mins cos i didnt want to face changing html in lotsa pages. I am now a convert to """ instead of chr$(34) cos it gets just too long when replacing lotsa html code

    B·O·L·L·O·X
    X·B·O·L·L·O
    O·X·B·O·L·L
    L·O·X·B·O·L
    L·L·O·X·B·O
    O·L·L·O·X·B
    B·O·L·L·O·X
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    n/a
    Posts
    89
    So plenderj, once i determin a line is in a file, how do i delete that line or word?
    n/a

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Use can use Replace().
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    DaoK
    Guest
    VB Code:
    1. Text1.text=Replace(Expression As String,Find As String,Replace As String,1,-1,vbTextCompare )

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    heh like the avatar
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    DaoK
    Guest
    Thx you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width