Recursion 不断地得到一个";“所需对象”;错误。尝试了一段时间的故障排除,但没有结果。

Recursion 不断地得到一个";“所需对象”;错误。尝试了一段时间的故障排除,但没有结果。,recursion,vbscript,Recursion,Vbscript,我一直在第32行“oFileCollection”中收到一个object required错误,我不确定问题的原因是函数没有从案例中接收到信息,还是函数需要包含整个参数和代码才能检索信息 Option Explicit Dim sDirectoryPath,Search_Days,iDaysOld,CmdArg_Object,lastModDate Dim oFSO,oFolder,oFileCollection,oFile,oTF, SubFolder '---------------

我一直在第32行“oFileCollection”中收到一个object required错误,我不确定问题的原因是函数没有从案例中接收到信息,还是函数需要包含整个参数和代码才能检索信息

    Option Explicit
Dim sDirectoryPath,Search_Days,iDaysOld,CmdArg_Object,lastModDate
Dim oFSO,oFolder,oFileCollection,oFile,oTF, SubFolder
'------------------------------------------------------

Set CmdArg_Object = Wscript.Arguments 

Select Case (CmdArg_Object.Count) 
    Case 2 
      sDirectoryPath = CmdArg_Object.item(0) 
      Search_Days = CmdArg_Object.item(1) 
    Case Else 
      WScript.Echo "SearchFiles.vbs requires 2 parameters:" &_ 
        vbcrlf & "1) Folder Path" &_ 
        vbcrlf & "2) # Days to Search" 
      WScript.Quit 
End Select 

Set oFSO = CreateObject("Scripting.FileSystemObject")
iDaysOld=Date+(-1*Search_Days)
Set oTF = oFSO.CreateTextFile("C:\Old Files.txt")

WScript.Echo Now & " - Beginning " & Search_Days & " day search of " & sDirectoryPath


TraverseFolders oFSO.GetFolder(sDirectoryPath)
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files

Function TraverseFolders (FolderName)

    Set SubFolder = oFileCollection 
    For Each SubFolder In FolderName.SubFolders
        TraverseFolders (SubFolder)
    Next


    For Each oFile In SubFolder.Files
        lastModDate = oFile.DateLastModified 
        If (lastModDate <= iDaysOld) Then
        oTF.WriteLine (oFile.Path)
        oTF.WriteLine (oFile.DateLastModified)
        oTF.WriteLine ("-----------------------")
        End If
    Next 

End Function
WScript.Echo "Now - Finished"
选项显式
Dim sDirectoryPath、搜索天数、iDaysOld、CmdArg\u对象、lastModDate
文件夹、文件夹、集合、文件、oTF、子文件夹的尺寸
'------------------------------------------------------
Set CmdArg_Object=Wscript.Arguments
选择案例(CmdArg\u Object.Count)
案例2
sDirectoryPath=CmdArg\u对象。项(0)
搜索天数=CmdArg\u对象。项(1)
其他情况
Echo“SearchFiles.vbs需要2个参数:&\u
vbcrlf和“1)文件夹路径”和
vbcrlf&“2)#搜索天数”
WScript.Quit
结束选择
Set of so=CreateObject(“Scripting.FileSystemObject”)
iDaysOld=日期+(-1*搜索天数)
设置oTF=oFSO.CreateTextFile(“C:\Old Files.txt”)
Echo Now&“开始”&Search\u Days&“天搜索”&sDirectoryPath
GetFolder的遍历文件夹(sDirectoryPath)
文件夹集=oFSO.GetFolder(sDirectoryPath)
集合oFileCollection=oFolder.Files
函数遍历文件夹(FolderName)
Set SubFolder=oFileCollection
对于FolderName.SubFolders中的每个子文件夹
遍历文件夹(子文件夹)
下一个
对于子文件夹.Files中的每个文件
lastModDate=oFile.DateLastModified

如果(lastModDate这是我的一个很老的例子;发布时保持不变

函数
ShowFolderListPlus
分析提供的文件夹,并为所有子文件夹递归调用自身

option explicit
Dim MyFolder, MyAgeLimitInDays, objFSO, numDateDiff, sResult
MyFolder = "C:\testC"
MyAgeLimitInDays = 365
sResult = "" 
Set objFSO = CreateObject( "Scripting.FileSystemObject")

ShowFolderListPlus( MyFolder)
WScript.Echo  "Testing files older/newer than " & Cstr( MyAgeLimitInDays) _
  & " days:" & vbNewLine & sResult
WScript.Quit 

Function ShowFolderListPlus( FolderToAnalyse)
  Dim objFolder, itemFile, itemFldr, colFileList, colSubFldr, parFolder, sc, sa
  sa = String( DepthOfPath( FolderToAnalyse), "-")
  sc = FolderToAnalyse & " " & sa & vbNewLine
  Set objFolder = objFSO.GetFolder( FolderToAnalyse)
  Set colFileList = objFolder.Files
  For Each itemFile in colFileList
    If StrComp( Right( itemFile.name, 4), ".bat", vbTextCompare) = 0 Then
      'exclude files of specified extension' 
    Else
      numDateDiff = DateDiff("d", itemFile.DateCreated, now) 
      If numDateDiff > MyAgeLimitInDays Then
        sc = sc & sa & "old "
        '''-------------------------------'''
        ''' objFSO.DeleteFile( itemFile)  ''' delete file older than limit  
        '''-------------------------------'''
      Else
        sc = sc & sa & "new "
      End If
      sc = sc & itemFile.name & " " & numDateDiff & vbNewLine
    End If
  Next
  Set colSubFldr = objFolder.SubFolders
  For Each itemFldr in colSubFldr
    parFolder = FolderToAnalyse & "\" & itemFldr.name
    ShowFolderListPlus( parFolder) 'calls the procedure itself recursively'
  Next
  sResult = sc & sResult
  ShowFolderListPlus = sc
End Function

Function DepthOfPath( strPth)
Dim AuxArray
AuxArray = Split( strPth, "\", -1, vbTextCompare)
DepthOfPath = UBound( AuxArray)
End Function
输出样本

==>cscript D:\VB\u scripts\Oldies\Folders\filescolection\u in_subfolders.vbs
测试超过365天的旧/新文件:
C:\testC-
-old bar.txt 777
-old foo.txt 777
C:\testC\NewFolder21--
--旧NewTextFile1 1289
--新的NewTextFile2 162
C:\testC\a--
C:\testC\43381802--
--旧MailCliönt.txt 582
--旧版Q4454519.html 538
C:\testC\43381802\bubu---
---新的2018年3月3日-.png 277
---旧NewTextDocument.txt 1146
---old output.txt 1146