Script to find folders and delete those that has been modified from over 8 days

Answered Script to find folders and delete those that has been modified from over 8 days

  • Friday, January 25, 2013 9:09 PM
     
      Has Code

    Hello everyone.

    I ask for help for the following:
    I'm trying to build a script that will locate folders on the network, and delete those who were last modified 8 days ago.

    On this script, he will delete every file with the name "Restore_*"

    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colFolders = objWMIService.ExecQuery _
        ("Select * from Win32_Directory Where FileName LIKE 'restore_%' and drive = 'e:'")
    
    For Each objFolder in colFolders
        Wscript.Echo objFolder.Name
    	objFolder.Delete(true)
        end if
    Next
    

    I've tried to add this, but it doesn't work.

    For Each objFolder in colFolders
        Wscript.Echo objFolder.Name
    
        if objFolder.LastModified < (Date() - 8) Then
    	objFolder.Delete(true)
        end if
    
    Next


    I guess this method doesn't work because the 'objFolder.LastModified' returns me this: 20121204190307.611007-120, wich is yyyymmddhhmmss.(I don't know what are the numbers afeter the dot).

    I have found this script here, but since I'm new in VBS, I don't know how to add those commands on my script.

    Const strPath = "R:\backup"
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Call Search (strPath)
    WScript.Echo"Done."
    
    Sub Search(str)
        Dim objFolder, objSubFolder, objFile
        Set objFolder = objFSO.GetFolder(str)
        For Each objSubFolder In objFolder.SubFolders
     If objSubFolder.DateLastModified < (Now() - 14) Then
          objSubFolder.Delete(True)
     End If
        Next
        For Each objSubFolder In objFolder.SubFolders
     Search(objSubFolder.Path)
        Next
    End Sub

    I appreciate if someone can help me.

    Thanks in advance.


    Att, Paulo Rocha

All Replies

  • Friday, January 25, 2013 9:58 PM
     
     Proposed Answer

    When you use the function ifobjFolder.LastModified < (Date() - 8) you assume that the difference is calculated in days. Why? It could be seconds, minutes, hours, days, months. To make your code work (and make it robust at the same time) you must nominate the unit you wish to consider, e.g. like so:

    if datediff("d", objFolder.LastModified, Date()) > 8 then . . .

    The downloadable help file script56.chm will tell you the various parameters for the DateDiff function.

  • Saturday, January 26, 2013 4:04 PM
     
     

    Thanks for replying.

    I have tried to use the DateDiff before, but I guess I must have done it wrong. I get run time error every time.

    I'm checking more information on the help file. Thanks.


    Att, Paulo Rocha

  • Saturday, January 26, 2013 4:15 PM
     
     

    I have tried to use the DateDiff before, but I guess I must have done it wrong. I get run time error every time.

    If you have a problem with the DateDiff function (or with any other function) then you should post two things:

    1. The relevant code that pertains to the problem.
    2. The exact error message.

    Without these details it is impossible to assist you.

  • Saturday, January 26, 2013 6:55 PM
     
     Answered Has Code

    I fixed my problem by converting the timestamp with this:

    Set objSWbemServices = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colTimeZone = objSWbemServices.ExecQuery ("SELECT * FROM Win32_TimeZone")
    For Each objTimeZone in colTimeZone
     strBias = objTimeZone.Bias
    Next
    
    dtmCurrDate = DATE()-8
    dtmTargetDate = Year(dtmCurrDate)
    
    dtmMes = Month(dtmCurrDate)
    If Len(dtmMes) = 1 Then
     dtmMes = "0" & dtmMes
    End If
    
    dtmTargetDate = dtmTargetDate & dtmMes
    
    dtmDia = Day(dtmCurrDate)
    If Len(dtmDia) = 1 Then
     dtmDia = "0" & dtmDia
    End If
    
    dtmTargetDate = dtmTargetDate & dtmDia & "000000.000000"
    dtmTargetDate = dtmTargetDate & Cstr(strBias)

    Now it's working 100%
    :)


    Att, Paulo Rocha

  • Saturday, January 26, 2013 7:18 PM
     
     Answered

    In my programming classes we got 50% of the marks when the code worked and another 50% for good structure, robustness and ease of maintenance. I recommend you grade this code one year from now. As an example, why use dtmCurrDate = DATE()-8 (where the meaning of "8" is not clear at all) instead of dtmCurrDate = DateAdd("d", -8, date())? Note also this concise method of left-padding a single-digit number: dtmDia = right("0" & Day(dtmCurrDate), 2). Why use four lines of code when one would do?

    What is the benefit of working out the Time Zone Bias and the string "000000.000000" that precedes it?

  • Saturday, January 26, 2013 7:44 PM
     
     Answered

    WMI dates are strings and cannot be used by DateDiff without conversion.

    WMI has a date helper object that converts and manages WMI date strings.  YOu should learn to use it as it will allow you to use date arithmetic in VBScript.

    See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393687(v=vs.85).aspx


    ¯\_(ツ)_/¯

  • Monday, January 28, 2013 10:15 AM
     
     
    Well, as I said before, I'm not very familiar with VBS yet. :)
    You were a great help!

    Thanks.

    Att, Paulo Rocha

  • Monday, January 28, 2013 10:21 AM
     
      Has Code
    Well, as I said before, I'm not very familiar with VBS yet. :)

    We understand that you are not very familiar with VBScript. What we puzzle over is why you ignore the advice you receive. VBScript has a couple of easy-to-use date manipulation functions. In your script you use neither but resort to some exotic WMI solution instead. Compare your latest code with your original code (with some some mods) to see what I mean:

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
    	("Select * from Win32_Directory Where FileName LIKE 'restore_%' and drive = 'e:'")
    
    For Each oFolder In colFolders
    	WScript.Echo "Folder name=" & oFolder.name
    	dFolderDate = oFSO.GetFolder(oFolder.name).DateLastModified
    '	If datediff("d", dFolderDate , Date()) > 8 Then objFolder.Delete(True)
    	If datediff("d", dFolderDate , Date()) > 8 Then WScript.Echo "Deleting   ", oFolder.name
    Next