Results 1 to 3 of 3

Thread: Read txt file[Resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Posts
    566

    Resolved Read txt file[Resolved]

    VB Code:
    1. Open "C:\myfile.txt" For Append As #1
    2.      Print #1, "blah"
    3. Close #1
    how can you read to check if "blah" already exists in the file? and if it does then don't write "blah" to it again.
    Last edited by emyztik; Sep 30th, 2005 at 11:46 PM.

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Read txt file

    VB Code:
    1. Dim MyStr as string
    2. Open "C:\myfile.txt" For binary As #1
    3.      MyStr = input(lof(1),1)
    4. Close #1
    5. If instr(MyStr,"blah") <> 0 then
    6. Open "C:\myfile.txt" For Append As #1
    7.      Print #1, "blah"
    8. Close #1
    9. End if

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Read txt file

    Read thru the file to see if it exists. Try this out:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim dup As Boolean
    5.   dup = CheckForDups("blah")
    6.   If dup = False Then
    7.     Open "C:\myfile.txt" For Append As #1
    8.        Print #1, "blah"
    9.     Close #1
    10.   End If
    11. End Sub
    12.  
    13. Function CheckForDups(s As String) As Boolean
    14.   Dim x As Integer, st As String
    15.   Dim ff As Integer
    16.   Dim strBuff As String
    17.   Dim str() As String
    18.   ff = FreeFile
    19.   Open "C:\myfile.txt" For Input As #ff
    20.     strBuff = Input(LOF(ff), ff)
    21.   Close #ff
    22.   str() = Split(strBuff, vbCrLf)
    23.   For x = 0 To UBound(str)
    24.     If s = str(x) Then
    25.       CheckForDups = True
    26.       Exit Function
    27.     End If
    28.   Next x
    29.   CheckForDups = False
    30. End Function

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