Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

    Unanswered: Issues with FileSearch

    I have the following code which is not returning any results.
    Code:
    With Application.FileSearch
            .NewSearch
            .LookIn = Application.CurrentProject.Path
            .SearchSubFolders = True
            .FileName = "Log.txt"
            .MatchTextExactly = True
            .Execute
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    EndWith
    Can anyone spot my error? I'm looking for a file called "Log.txt" which is in the same folder as my mdb file - yet is never returned when the above is run.
    I'm all confussled
    -George
    George
    Home | Blog

  2. #2
    Join Date
    Aug 2006
    Posts
    559
    georgev,

    i don't know if this will help or mess you up, I found it in a book I have. It opens a file (txt) using ADO:

    Code:
    Sub Open_TextFile()
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim fld As ADODB.Field
    Set conn = New ADODB.Connection
    Debug.Print conn.ConnectionString
    conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & _
    "DBQ=" & CurrentProject.Path & "\"
    Set rst = New ADODB.Recordset
    rst.Open "select * from [Employees.txt]", conn, adOpenStatic, _
    adLockReadOnly, adCmdText
    Do Until rst.EOF
    For Each fld In rst.Fields
    Debug.Print fld.Name & "=" & fld.Value
    Next fld
    rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    conn.Close
    Set conn = Nothing
    MsgBox "Open the Immediate window to view the data."
    End Sub

    *****EDIT*****

    georgev,

    I don't know if the attachment will help you better. But i can keep looking.
    Attached Files Attached Files
    Last edited by Grafixx01; 06-06-07 at 10:31.

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Thanks for the idea, but I currently use a simpler method of writing to a text file
    Code:
    Dim fs, a As Object
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set a = fs.OpenTextFile(ProjectPath & "\Log.txt", 8, -2)
        a.Write Text & vbCrLf
        a.Close
    The 8 signifies that we are appending to the text file. The -2 says to use the regional settings for the method of opening the file (ASCII or Unicode).

    I'm just having problems with the FileSearch
    Basically I'm trying to work out if the file exists; if it does then write to it, if it doesn't create it and then write to it.
    George
    Home | Blog

  4. #4
    Join Date
    May 2005
    Posts
    1,191
    Quote Originally Posted by georgev
    I'm just having problems with the FileSearch
    Basically I'm trying to work out if the file exists; if it does then write to it, if it doesn't create it and then write to it.
    This is some older code I wrote back in the day to check if a file exists, don't know if it'll help you or not.

    Code:
    Dim strDatabaseFolder As String
    strDatabaseFolder = Application.CurrentProject.Path
    
    Dim strFilePath As String
    strFilePath = strDatabaseFolder & "\log.txt"
    
    Dim blnFileExists As Boolean
    Set fsoFile = CreateObject("Scripting.FileSystemObject")
    blnFileExists = fsoFile.FileExists(strFilePath)
    
    If blnFileExists Then
        MsgBox "This file exists."
    Else
        MsgBox "You're a failure at life."
    End If
    EDIT: Simplified code
    Last edited by nckdryr; 06-06-07 at 10:42.
    Me.Geek = True

  5. #5
    Join Date
    May 2005
    Posts
    1,191
    Quote Originally Posted by georgev
    I have the following code which is not returning any results.
    Code:
    With Application.FileSearch
            .NewSearch
            .LookIn = Application.CurrentProject.Path
            .SearchSubFolders = True
            .FileName = "Log.txt"
            .MatchTextExactly = True
            .Execute
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    EndWith
    Can anyone spot my error? I'm looking for a file called "Log.txt" which is in the same folder as my mdb file - yet is never returned when the above is run.
    I'm all confussled
    -George
    I plugged in the code to a db of mine, and I just saw it: last line needs to be End With, Gotta love those stupid picky syntaxes.

    You're welcome in advance
    Me.Geek = True

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Apologies - that's simply a typo when uploading to the site - not contained in my database code! I'm lpaying with your method know - customizing it to my requirements! Will let you know how I get on

    Cheers Nick and Co.
    George
    Home | Blog

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    C'etait perfect!
    Here's my final solution
    I can't believe I never found the .FileExists - thanks for that Nick
    Code:
        Set fs = CreateObject("Scripting.FileSystemObject")
        FileExist = fs.FileExists(ProjectPath & "\Log.txt")
        
        If Not FileExist Then
            Set a = fs.CreateTextFile(ProjectPath & "\Log.txt", True)
            a.WriteLine "Log created: " & Chr(9) & Now() & vbCrLf
            a.Close
        End If
    
        Set a = fs.OpenTextFile(ProjectPath & "\Log.txt", 8, -2)
        a.Write "this is working" & vbCrLf
        a.Close
    George
    Home | Blog

  8. #8
    Join Date
    May 2005
    Posts
    1,191
    Besides the End With statement, why didn't your previous code work? What error were you getting? I was able to plug it into a blank database on my desktop, put a txt file in a new folder on the desktop, and it found the file just fine.
    Me.Geek = True

  9. #9
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    It always returned a filecount of zero...
    I don't know why, but I much prefer your approach anyway - cleaner and easier to understand!
    George
    Home | Blog

  10. #10
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    If you are happy then all good & ignore the below.

    I tend(ed) to use filesystem object too. Izy reckons the inbuilt file functions are superior (he could elaborate on why - I forget).
    Shorter code that does the same:
    Code:
    Open ProjectPath & "\Log.txt" For Append Shared As #1
    Write #1, "this is working"
    Close #1
    It does everything but add your header. You can also use Dir() to see if a file exists.

    BTW- I presume you no longer need to search subdirectories?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  11. #11
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I do like the above Open method simply because it appears much simpler. However
    Code:
    Dim FileNumber As Integer
        
        FileNumber = FreeFile
        Open ProjectPath & "\Log.txt" For Append Shared As #FileNumber
        Write #FileNumber, "this is working..."
        Close #FileNumber
    Returns this in my Log file (note the double quotes)
    Quote Originally Posted by Log.txt
    "this is working..."
    Not a huge issue but is there a way to not input the value in quotes?

    You also mentioned the Dir() function - would this be the correct use:
    Code:
    If Dir(ProjectPath & "\Log.txt") = "" Then
        'File does not exist
    Else
        'File exists
    End If
    Thank you all for your help and advice
    George
    Home | Blog

  12. #12
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by georgev
    You also mentioned the Dir() function - would this be the correct use:
    Code:
    If Dir(ProjectPath & "\Log.txt") = "" Then
        'File does not exist
    Else
        'File exists
    End If
    Yes.

    I'll get to you re t'other
    Testimonial:
    pootle flump
    ur codings are working excelent.

  13. #13
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Get me to what now?
    George
    Home | Blog

  14. #14
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quotes

    And no - there does not appear to be a way.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  15. #15
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Mis-read... I missed out the "to"
    I't's a shame they add these crappy quotes, because I'm adding vbCrLf's and it's not so pretty having lines that just contain a double quote.
    George
    Home | Blog

Posting Permissions

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