尝试运行以下代码时,我收到“Object required”运行时错误 . 我试图找到保存在文件夹中的最新文件,并将其复制/重命名为另一个文件名 . 我的问题是“检查文件夹中的最新文件”的If语句是返回对象错误 .
我在这里已经遵循了其他建议,但我似乎无法克服对象错误障碍 . 如果有人可以为这个新手提供帮助,我将不胜感激 .
Option Explicit
Dim FSO, FSO2, FLD, FIL
Dim strFolder, strContent, strPath, tmpName, tmpName2, mostRecent, newfile
strFolder = "C:\Users\username\Documents\Mockup"
'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO2 = CreateObject("Scripting.FileSystemObject")
'loop through the folder and get the files
For Each FLD in FSO.GetFolder(strFolder).SubFolders
'Reset mostRecent to nothing
Set mostRecent = Nothing
Set mostRecent = CreateObject("Scripting.FileSystemObject")
For Each Fil In FLD.Files
If Fil = FLD & "\import.ARCHIVE" Then
FSO2.DeleteFile FLD & "\import.ARCHIVE"
Fil = FLD & "\import.tra"
End If
If Fil = FLD & "\import.tra" Then
tmpName = Replace(Fil, "import.tra", "import.ARCHIVE")'Replace(String, thisString, toThisString)
'Name Fil as tmpName
FSO.MoveFile Fil, tmpName
End If
'Check for most recent file in folder
If mostRecent Is Nothing Then
Set mostRecent = Fil
ElseIf Fil.DateCreated > mostRecent.DateCreated Then
Set mostRecent = Fil
End If
tmpName2 = Replace(mostRecent, "*.*", "import.tra")'Replace(String, thisString, toThisString)
FSO2.CopyFile mostRecent, tmpName2
Next
Next
'Clean up
Set FLD = Nothing
Set FSO = Nothing
Set FSO2 = Nothing
Set mostRecent = Nothing
1 回答
只是掩码
Set mostRecent = CreateObject("Scripting.FileSystemObject")
会做的 .我们没有't need a File System Object to get the date created for a file. I think you just mean to create an object. In vbscript, since you already ' dim mostRecent',当使用
Set
时,它会自动分配给一个对象 .希望对你有帮助 :)