[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need this script to check just the date on folders

Posted on 2011-04-20
4
Medium Priority
?
228 Views
Last Modified: 2012-08-13
Hi, The script below works, but it deletes everything of that age. What I want it to do is check the dates on the folders directly under the "Done" directory. Not files in those subfolders or subfolders of those folders.  (Ie: done\test but not done\test\test1)
And delete the whole subfolder recursively.
Current script:
****************************************************************
Option Explicit
Dim fpath, fcount,fsoc,logname ,myday,mymonth
Dim daystokeep
daystokeep=182
fpath="D:\departments\TPD\Private\Change\Done"


Const ForAppending = 8 'Scripting.IOMode
myday=Day(DATE)
if len(myday)=1 then myday= "0" & myday
mymonth=month(DATE)
if len(mymonth)=1 then mymonth= "0" & mymonth
logname="D:\Private\Change\Done\Deleted" & myday & myMonth & Right(Year(DATE),2) & ".log"

Set FSOC = CreateObject("Scripting.FileSystemObject")

writelog "*************************************"
writelog "Start Purge of: " & fpath & " " & now()
writelog "Deleting Files older than: " & daystokeep & " Day(s)" & VbCrLf

ShowSubfolders FSOC.GetFolder(fpath)

Sub ShowSubFolders(Folder)
Dim subfolder
on error resume next
    For Each Subfolder in Folder.SubFolders
        ShowSubFolders Subfolder
        delfiles subfolder
        if subfolder.SubFolders.count=0 AND subfolder.files.count=0 then
           writelog "Folder " & subfolder & " Deleted"
         fsoc.deletefolder(subfolder)
           DisplayErrorInfo
        end if      
    Next
End Sub
*******************************************************************************8
Thanks
Mike
0
Comment
Question by:bentham1
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35434313
I don't currently see any checking of the age of files or folders, it just seems to be removing empty folders.

Do you want to delete the files under the done folder that are a cetain age, or only look at the date of the first level of subfolders under the done folder, and when they are old (based on last updated date?) remove that folder and all children?

A little confusing based on your post of:

 (Ie: done\test but not done\test\test1)
And delete the whole subfolder recursively.

which sounds like you want to remove the entire done\test folder, but then you also say not the \done\test\test1 folder?

!bp
0
 

Author Comment

by:bentham1
ID: 35435911
Hi,
Yes, I would like to check the age of of the top level folders directly under the done folder and then delete the any of those folders and their children if the top level folder is older than 182 days.

Sorry I meant check the age of Done\test not \done\test\test1. But delete test and everything under it if  test is over 182 days.
0
 
LVL 12

Assisted Solution

by:prashanthd
prashanthd earned 400 total points
ID: 35437511
Try the following

regards
Prashanth
Option Explicit
Dim fpath, fcount,fsoc,logname ,myday,mymonth
Dim daystokeep
daystokeep=182
fpath="D:\departments\TPD\Private\Change\Done"


Const ForAppending = 8 'Scripting.IOMode
myday=Day(Date)
If Len(myday)=1 Then myday= "0" & myday
mymonth=Month(Date)
If Len(mymonth)=1 Then mymonth= "0" & mymonth
logname="D:\Private\Change\Done\Deleted" & myday & myMonth & Right(Year(Date),2) & ".log"

Set FSOC = CreateObject("Scripting.FileSystemObject")

writelog "*************************************"
writelog "Start Purge of: " & fpath & " " & Now()
writelog "Deleting Files older than: " & daystokeep & " Day(s)" & vbCrLf

ShowSubfolders FSOC.GetFolder(fpath)

Sub ShowSubFolders(Folder)
    Dim subfolder
    On Error Resume Next
    For Each Subfolder In Folder.SubFolders
            fsoc.DeleteFolder subfolder,True
            writelog "Folder " & subfolder & " Deleted"
            DisplayErrorInfo
    Next
End Sub

Open in new window

0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 600 total points
ID: 35437822
I think this script will do what you want.  I added the date checking for the subfolders, and cleaned up the logging.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

intDaysToKeep = 182
strBaseDir = "D:\departments\TPD\Private\Change\Done"
strDateStamp = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
strLogName = "D:\Private\Change\Done\Deleted_" & strDateStamp & ".log"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objLogFile = objFSO.OpenTextFile(strLogName, ForAppending, True)

objLogFile.WriteLine "*************************************"
objLogFile.WriteLine "Purge started at: " & Now()
objLogFile.WriteLine "Purging folder in: [" & strBaseDir & "]"
objLogFile.WriteLine "Deleting folders older than: [" & intDaysToKeep & "] Day(s)"

For Each objSubFolder In objFSO.GetFolder(strBaseDir).SubFolders
   If DateDiff("d", objSubFolder.DateLastModified, Date) > intDaysToKeep Then
      objLogFile.WriteLine "Deleting subfolder: [" & objSubFolder.Name & "]"
      objSubFolder.Delete
   End If
Next

objLogFile.Close

objLogFile.WriteLine "Purge ended at: " & Now()
objLogFile.WriteLine "*************************************"

Open in new window

~bp
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question