Beats me - Help needed with folder object and output redirection

Author Message
sun1010

  • Total Posts : 27
  • Scores: 0
  • Reward points : 0
  • Joined: 1/11/2010
  • Status: offline
Beats me - Help needed with folder object and output redirection Wednesday, February 01, 2012 10:35 PM (permalink)
0
[Helpful answer received] / [List Solutions Only]
Hi,
 
I'm working on a script to delete all files and folders that are older than a specific number of days (for example, 60 days). So the script will recurse through all folders for the given input folder, check how old it is, and then delete it. Also, if a folder is empty, it deletes it.
 
The problem is, whenever it is encountering the very first empty folder, it is deleting it, and then immediately exiting out of the script without printing anything. So basically at the first sign of empty folder the script is simply dying.
 
Here are the bits for iterating through the folders and deleting them.
 
' This line is in the main program which calls the function ShowSubFolders
ShowSubFolders objFSO3.GetFolder(pathtofolder1)

 
' This is the function ShowSubFolders
 
 Sub ShowSubFolders(Folder)
 For Each Subfolder in Folder.SubFolders 
 Set objFolder = objFSO3.GetFolder(Subfolder.Path)
 
 ' Enumerating folders 
 objLogfile.Write(Subfolder.Name & ",")
 If Subfolder.Size = 0 Then 
 temppath = Subfolder.Path
 objFSO3.DeleteFolder Subfolder.Path ' DELETES the empty folder
 If Err.Number = 0 Then 
 objLogfile.Write("Folder - EMPTY," & temppath & "," _ 
 & Subfolder.DateLastAccessed & ",-,Deleted" _ 
 & vbCrLf) 
 Else 
 objLogfile.Write("Folder - EMPTY," & temppath & "," _ 
 & Subfolder.DateLastAccessed & ",-,Error Deleting" _ 
 & vbCrLf) 
 
 End If 
 Else 
 objLogfile.Write("Folder - NOT Empty," & objFolder.Path _
 & "," & Subfolder.DateLastAccessed & ",-,-," _ 
 & vbCrLf) 
 End If 
 
 
 
 ' Enumerating files 
 
 Set colFiles = objFolder.Files
 For Each objFile in colFiles

 intDaysDifference = DateDiff("d", objFile.DateLastAccessed, Date)

 objLogfile.Write (objFile.Name & "," & "File" & "," _ 
 & Subfolder.Path & "," & objFile.DateLastAccessed & "," _
 & intDaysDifference & " days" & ",")
 
 If Abs(intDaysDifference) > Abs(numberofdays) Then 
 objFSO3.DeleteFile Subfolder.Path & "\" & objFile.Name
 objLogfile.Write ("Deleted" & vbCrLf) 
 Else 
 objLogfile.Write ("No" & vbCrLf) 
 End If 
 
 Next
 ShowSubFolders Subfolder
 Next
End Sub

 

 
It's deleting the folder as intended, but not sure why it is not printing the output and simply exiting.
 
Any pointers?
 
 
Cheers
<message edited by sun1010 on Wednesday, February 01, 2012 10:47 PM>
 
#1
    59cobalt

    • Total Posts : 969
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Beats me - Help needed with folder object and output redirection Thursday, February 02, 2012 1:17 AM (permalink)
    5
    [This post was marked as helpful]
    sun1010
    The problem is, whenever it is encountering the very first empty folder, it is deleting it, and then immediately exiting out of the script without printing anything. So basically at the first sign of empty folder the script is simply dying.
    [...]
    objFSO3.DeleteFolder Subfolder.Path ' DELETES the empty folder
    If Err.Number = 0 Then
    objLogfile.Write("Folder - EMPTY," & temppath & "," _
     & Subfolder.DateLastAccessed & ",-,Deleted" _
     & vbCrLf)

    It's deleting the folder as intended, but not sure why it is not printing the output and simply exiting.
    Your script is dying when it tries to access the .DateLastAccessed property of the object it just deleted. How do you expect to be able to access a property of an object that doesn't exist anymore?
     
    #2
      sun1010

      • Total Posts : 27
      • Scores: 0
      • Reward points : 0
      • Joined: 1/11/2010
      • Status: offline
      Re:Beats me - Help needed with folder object and output redirection Thursday, February 02, 2012 3:13 AM (permalink)
      0
      Dear 59cobalt,
       
      Thanks a ton. A logical error on my part. Thanks for your help. You are a star.. :)
       
      I've taken care of that now. However, the very next bump is that, after deleting the empty folder, it's printing that it has deleted the folder and then exiting the script.
       
      What probably is happening is, that after deleting the Empty folder, the variable "objFolder" becomes unusable. So the script is unable to look into this folder which doesn't exist. I hope this makes sense.
       
      What needs to be done is that, if a folder is Empty, delete it, and then go for the next "For Each" item. Is there any way of doing that?
       
       Sub ShowSubFolders(Folder)
       For Each Subfolder in Folder.SubFolders 
       Set objFolder = objFSO3.GetFolder(Subfolder.Path)
       
       ' Enumerating folders 
       objLogfile.Write(Subfolder.Name & ",")
       If Subfolder.Size = 0 Then
       temppath = Subfolder.Path
       'tempaccessdate = Subfolder.DateLastAccessed
       objFSO3.DeleteFolder Subfolder.Path ' DELETES the empty folder
       If Err.Number = 0 Then 
       objLogfile.Write("Folder - EMPTY," & temppath & "," _ 
       & "-,-,Deleted" _ 
       & vbCrLf) 
       Else 
       objLogfile.Write("Folder - EMPTY," & temppath & "," _ 
       & Subfolder.DateLastAccessed & ",-,Error Deleting" _ 
       & vbCrLf) 
       End If 
      
       Else 
       objLogfile.Write("Folder - NOT Empty," & objFolder.Path _
       & "," & Subfolder.DateLastAccessed & ",-,-," _ 
       & vbCrLf) 
       End If 
       
       
       
       ' Enumerating files 
       
       Set colFiles = objFolder.Files
       For Each objFile in colFiles
      
       intDaysDifference = DateDiff("d", objFile.DateLastAccessed, Date)
      
       objLogfile.Write (objFile.Name & "," & "File" & "," _ 
       & Subfolder.Path & "," & objFile.DateLastAccessed & "," _
       & intDaysDifference & " days" & ",")
       
       If Abs(intDaysDifference) > Abs(numberofdays) Then 
       objFSO3.DeleteFile Subfolder.Path & "\" & objFile.Name
       objLogfile.Write ("Deleted" & vbCrLf) 
       Else 
       objLogfile.Write ("No" & vbCrLf) 
       End If 
       
       Next
       
       ShowSubFolders Subfolder
       
       Next
      End Sub
       

       
      #3
        sun1010

        • Total Posts : 27
        • Scores: 0
        • Reward points : 0
        • Joined: 1/11/2010
        • Status: offline
        Re:Beats me - Help needed with folder object and output redirection Thursday, February 02, 2012 3:21 AM (permalink)
        0
        Just an update. I added "On Error Resume Next" just inside the For Each, and now it's going forward to the next folder. Although it's printing a few lines of "No", I guess I can debug that.
        <message edited by sun1010 on Thursday, February 02, 2012 3:23 AM>
         
        #4
          59cobalt

          • Total Posts : 969
          • Scores: 91
          • Reward points : 0
          • Joined: 7/17/2011
          • Status: offline
          Re:Beats me - Help needed with folder object and output redirection Thursday, February 02, 2012 7:52 AM (permalink)
          0
          Apparently you do get an error there, so you should add some debugging code to find out what the error is:
          ' some code
          
          On Error Resume Next
          ' problematic command
          If Err.Number <> 0 Then WScript.Echo Err.Description & " (0x" & Hex(Err.Number) & ")"
          On Error Goto 0
          
          ' more code

           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9