Office中国论坛/Access中国论坛

标题: 如何用代码找出文件夹中最早保存的文件名? [打印本页]

作者: 小铁匠    时间: 2008-1-29 17:03
标题: 如何用代码找出文件夹中最早保存的文件名?
找出文件夹中最早保存的文档的代码怎么写,请高手指教!
作者: 小铁匠    时间: 2008-1-29 21:19
Private Sub Form_Load()
Dim conn As ADODB.Connection
    Set conn = CurrentProject.Connection
    Dim rst As New ADODB.Recordset
    Dim strsql As String
    Dim TemPath As String
    Dim strDBFileName As String
    strsql = "SELECT  top 1 源文件, 备份文件 FROM 备份"
    rst.Open strsql, conn, 1, 1
if not rst.eof then
If Not IsNull(rst("源文件")) And Not IsNull(rst("备份文件")) Then
        strDBFileName = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & ".mdb"
       TemPath = rst("备份文件") & "\"
          FileCopy rst("源文件"), TemPath & strDBFileName
    End If
end if
   rst.Close
    Set rst = Nothing
    Set conn = Nothing
End Sub
以上是我备份的代码我想在这代码中加入只保存20个备份的循环保存方法,就是在 rst("备份文件") 的文件夹中如果超过20个就删除第一个保存的文档。
        Set fs = Application.FileSearch
        fs.LookIn = rst("备份文件")
        fs.FileName = "*.mdb"
if fs.FoundFiles.Count >=20 then  删除第一个保存的文档,请高手指教完整

[ 本帖最后由 小铁匠 于 2008-1-29 21:25 编辑 ]
作者: tyhtyq    时间: 2008-1-29 23:38
值得详细研究一下!谢谢
作者: 小铁匠    时间: 2008-1-30 11:35
没人给我回复吗?求教高手指点!!!
作者: andymark    时间: 2008-1-30 12:50
Dim TemPath As String
Dim StrKey As String
Dim Tem As String
Set fs = Application.FileSearch
TemPath = CurrentProject.Path & "\bak\"

With fs

.LookIn = CurrentProject.Path & "\bak"
.FileName = "*.mdb"

If .Execute > 0 Then

IntTem = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now())

If .FoundFiles.Count > 3 Then

For I = 1 To .FoundFiles.Count

StrKey = Right(fs.FoundFiles(I), Len(fs.FoundFiles(I)) - Len(TemPath)) 'filename
StrKey = Left(StrKey, Len(StrKey) - 4)


If IntTem > StrKey Then
IntTem = StrKey
End If

Next I

If IntTem <> Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) Then
Kill TemPath & IntTem & ".mdb"
End If

End If

End If
End With

作者: andymark    时间: 2008-1-30 12:52
If .FoundFiles.Count > 3 Then   这里根据情况修改




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3