-1

I keep receiving an object required error on line 32 for "oFileCollection" and am unsure if the cause of the problem is the function not receiving the information from the Case or if the function needs to have the whole argument and code inside of it in order to retrieve the information.

    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"
A.P.
  • 3
  • 3
  • Possible duplicate of [Recursively access subfolder files inside a folder](https://stackoverflow.com/questions/14950475/recursively-access-subfolder-files-inside-a-folder) – user692942 Dec 05 '18 at 21:19

1 Answers1

0

Here's my quite old example; posted as is unchanged…

Function ShowFolderListPlus analyzes supplied folder and calls itself recursively for all subfolders.

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

Output sample:

==> cscript D:\VB_scripts\Oldies\Folders\filescolection_in_subfolders.vbs
Testing files older/newer than 365 days:
C:\testC -
-old bar.txt 777
-old foo.txt 777
C:\testC\NewFolder21 --
--old NewTextFile1 1289
--new NewTextFile2 162
C:\testC\a --
C:\testC\43381802 --
--old MailCliеnt.txt 582
--old q44554519.html 538
C:\testC\43381802\bubu ---
---new 3-3-2018-.png 277
---old NewTextDocument.txt 1146
---old output.txt 1146
JosefZ
  • 28,460
  • 5
  • 44
  • 83