我有兩個下面提到的潛艇。
第一個子獲取檔案修改日期并且它正在作業。
第二個 sub 將檔案從一個檔案夾移動到另一個檔案夾。是否可以將這兩個潛艇合并在一起,以便它首先找到最早的修改時間,然后相應地移動它?
但是,也請幫助在其中包含一個回圈。
Sub test()
Dim FSO As Object
Dim fol As Object
Dim fil As Object
Dim temp As Date
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fol = FSO.GetFolder("E:\Source")
For Each fil In fol.Files
temp = fil.DateLastModified
Next fil
MsgBox temp
End Sub
Sub FSOMoveAllFiles()
Dim FSO As New FileSystemObject
Dim FromPath As String
Dim ToPath As String
Dim FileInFromFolder As Object
FromPath = "E:\Source\"
ToPath = "E:\Destination\"
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
FileInFromFolder.Move ToPath
Next FileInFromFolder
End Sub
uj5u.com熱心網友回復:
請測驗下一個函式,能夠回傳最舊的檔案(根據其創建時間):
Function OldestFile(strFold As String) As String
Dim FSO As Object, Folder As Object, File As Object, oldF As String
Dim lastFile As Date: lastFile = Now
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(strFold)
For Each File In Folder.files
If File.DateCreated < lastFile Then
lastFile = File.DateCreated: oldF = File.name
End If
Next
OldestFile = oldF
End Function
如果您需要最后修改的檔案,則應替換File.DateCreated
為File.Datelastmodified
.
應該通過以下方式從您的代碼中呼叫它:
Sub MoveOldestFile()
Dim FromPath As String, ToPath As String, fileName As String
FromPath = "E:\Source\"
ToPath = "E:\Destination\"
fileName = OldestFile(FromPath)
If Dir(ToPath & fileName) = "" Then
Name FromPath & fileName As ToPath & fileName
Else
MsgBox "File """ & fileName & """ already moved..."
End If
End Sub
每次運行上述代碼時,將最舊的檔案移動到“ToPath”檔案夾。
請在測驗后發送一些反饋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528677.html
標籤:vba文件系统对象