这是我使用的子程序的摘录:
Private Sub DeleteFilesNotCreatedToday(myTargetFolder As String)
Dim myFolder
Dim myFile
Dim YesterdayDate As Date
YesterdayDate = Date
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder(myTargetFolder).Files
For Each myFile In myFolder
...
...
Next myFile
为什么我不能更具体地声明myFolder
而不是变体 - 或者我可以吗?我已经尝试将它声明为一个集合,但它错了......
Private Sub DeleteFilesNotCreatedToday(myTargetFolder As String)
Dim myFolder As Collection
Dim myFile
Dim YesterdayDate As Date
YesterdayDate = Date
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder(myTargetFolder).Files
For Each myFile In myFolder
...
...
Next myFile
答案 0 :(得分:2)
设置对microsoft脚本运行时的引用,并将其声明为Dim myFolder As Scripting.Folder
修改
要获取文件集合,请使用脚本运行时库中的文件集合。示例如下所示。
Sub CollectFiles()
Dim myFile As Scripting.File
Dim myFolder As scripting.folder
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\")
For Each myFile In myFolder.Files
Next myFile
End Sub
Edit2:如果你想将myFolder标注为文件集合而不是文件夹,你可以通过以下方式实现:
Sub CollectFiles()
Dim myFile As Scripting.File
Dim myFolder As Scripting.Files
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\").Files
For Each myFile In myFolder
Next myFile
End Sub
如果人们希望它是文件夹而不是文件集合,可能会有点混乱,所以最好重命名它。