?
Solved

MS Access Application.FileSearch

Posted on 2011-03-22
4
Medium Priority
?
458 Views
Last Modified: 2013-11-27
After searching I found the problem is that 07 no longer supports the .FileSearch function (which is stupid IMO).  Anyway, I have code that worked in 03.  It searches for .log files in a directory and if there are more than 10 files present, it deleted the oldest one.  What do I need to use in 07 to do this?

Here is the code that worked in 03.

Thank you!

Sub DeleteOldestFile()
'maintains the number of log files to a max of 10.  Deletes the oldest file.
    Dim strFilePath As String

    strFilePath = GetDBPath
   
    With Application.FileSearch
       .NewSearch
       .LookIn = strFilePath
       .Filename = "*.log"
       .Execute
       If .FoundFiles.Count >= MAX_LOG_COUNT Then
            Kill .FoundFiles(1)
       End If
    End With
   
End Sub

Open in new window

0
Comment
Question by:AronMcD
  • 2
  • 2
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35189762
you can try the File scripting, needs reference to Microsoft scripting runtime

codes goes something like this

Dim fso As Scripting.FileSystemObject
Dim srcFolder As Scripting.Folder
Dim fileItem As Scripting.File
Dim strFilePath As String

    strFilePath = GetDBPath

Set fso = New Scripting.FileSystemObject
Set srcFolder = fso.GetFolder(strFilePath)

    For Each fileItem In srcFolder.Files
        If Right(fileItem, 4) = ".log" Then
        Debug.Print fileItem.Name, fileItem.DateCreated
        End If
    Next
    

Open in new window

0
 

Author Comment

by:AronMcD
ID: 35190107
capricorn,
Using fileItem.Delete deletes all files with the .log extension.  How do I delete just the oldest file?  

Thanks for the help.

0
 

Author Comment

by:AronMcD
ID: 35190334
capricorn,
I got it to work using:

Kill strFilePath & fileItem.Name

Seems to work fine.....unless you know of a better way.

Thanks for the help.   I appreciate it.  
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35190534
sorry, just got back from a meeting. glad you got it working..
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

862 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