Vbscript 尝试查找最近的文件并复制它时出错
我在尝试运行以下代码时遇到“需要对象”运行时错误。我正在尝试查找保存在文件夹中的最新文件,并将其复制/重命名为其他文件名。我的问题是“检查文件夹中最近的文件”的If语句返回对象错误 我在这里遵循了其他建议,但我似乎无法克服对象错误的障碍。如果有人能为这个新手提供帮助,我将不胜感激Vbscript 尝试查找最近的文件并复制它时出错,vbscript,Vbscript,我在尝试运行以下代码时遇到“需要对象”运行时错误。我正在尝试查找保存在文件夹中的最新文件,并将其复制/重命名为其他文件名。我的问题是“检查文件夹中最近的文件”的If语句返回对象错误 我在这里遵循了其他建议,但我似乎无法克服对象错误的障碍。如果有人能为这个新手提供帮助,我将不胜感激 Option Explicit Dim FSO, FSO2, FLD, FIL Dim strFolder, strContent, strPath, tmpName, tmpName2, mostRecent, ne
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
只需掩码
Set mostRecent=CreateObject(“Scripting.FileSystemObject”)
即可
我们不需要文件系统对象来获取文件的创建日期。我想你只是想创建一个对象。在vbscript中,由于您已经“调暗最深”,因此在使用Set
时,它将自动指定给对象
希望这对您有所帮助:)请显示准确的错误消息和引发错误的行。