Jump to content

Script to delete all folders older then 2 days.


Recommended Posts

Posted

I currently have a script that I run a task for weekly to delete log files. I need to modify it to delete all folders and their contents within a certain directory that are over 2 days old. I cant quite figure it out. Any help would be appreciated. This is what I use for the file deletion:

 

' SET THE DROPDATE VARIABLE TO TODAYS DATE MINUS 30 DAYS

dropdate = dateadd("d", -30, date())

 

' CREATE A FILESYSTEM OBJECT

set fso = createobject("scripting.filesystemobject")

 

' CREATE A FOLDER OBJECT (USING THE FILESYSTEM OBJECT)

set folders = fso.getfolder("C:\WINDOWS\system32\LogFiles\W3SVC1")

 

' CREATE A FILES OBJECT (USING THE FOLDER OBJECT THAT'S USING THE FILESYSTEM OBJECT.)

set files = folders.files

 

' LOOP THROUGH EACH FILENAME IN THE FILES OBJECT

for each file in files

 

' COMPARE THE FILES 'DATE LAST MODIFIED' PROPERTY

' IF THE FILES 'DATE LAST MODIFIED' IS MORE THAN DROPDATE, THEN ECHO SOME OUTPUT AND DELETE THE FILE

if file.datelastmodified < dropdate then

 

'wscript.echo folders.path & "\" & file.name & " last modified: " & file.datelastmodified

file.delete

 

on error resume next

 

end if

 

next

Posted

Why not use Xcopy or Dir with the /d option [ date ] to copy / List the relevant files first, then delete them afterwards.....? [ whilst Xcopying / Listing them, pipe the contents to a .txt file, tidy up the .txt file, then use that as a template / answer file to insert the delete command......

 

[ Bit of a long way round using Batch - I was personally use Autoit with the FileGetTime function..... ]

Posted

Open Notepad, copy and paste, save, rename to 48hrdelete.vbs

Amend the path {Win32_Directory.Name='C:\WINNT\system32\spool\PRINTERS'} as necessary.

 

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='C:\WINNT\system32\spool\PRINTERS'} Where " _
       & "ResultClass = CIM_DataFile")

strCurrentDate = Now

For Each objFile In colFiles
   strFileDate = WMIDateStringToDate(objFile.CreationDate)
   intHours = DateDiff("h", strFileDate, strCurrentDate)
   If intHours >= 48 Then
       Wscript.Echo objFile.Delete
   End If
Next

Function WMIDateStringToDate(dtmInstallDate)
   WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
       Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
           & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
               Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                   13, 2))
End Function

 

S'what I use to empty printjobs out that are more than 2 days old that have gotten stuck somewhere in the queue...

 

Call it using a batch file so you can stop and restart services...

 

@echo off
echo Stopping TCP/IP Print Server
net stop "TCP/IP Print Server"
echo Stopping Print Spooler
net stop "Print Spooler"
echo
echo Deleting all spool files older than 48 hours
cscript 48hrdelete.vbs
echo
echo Restarting Print Spooler
net start "Print Spooler"
echo Restarting TCP/IP Print Server
net start "TCP/IP Print Server"
echo Complete

 

God knows where the vbs came from - took some hunting on Google at the time... butcher as required if useful.

  • Thanks 1
Posted

Here's a vbscript I use to delete log files. I'm sure you can tweak it for your needs:

 

'==========================================================================

'

' NAME: DeleteLogFiles.vbs

'

' AUTHOR: Jim Williams

' DATE : 07/08/2006

'

' COMMENT: Deletes log files (or any other type) by age. Uses the file

' creation date to specify file to delete.

'==========================================================================

Option Explicit

 

' Set constants for log file paths and maximum age of files

const LogPath1 = "\\server6\c$\winnt\system32\logfiles\w3svc1"

const LogPath2 = "\\server6\c$\WINNT\system32\LogFiles\W3SVC3"

Const LogFileAge = 30

 

'WScript.Echo LogPath1

DeleteFilesByDate logpath1, LogFileAge

'WScript.Echo logpath2

DeleteFilesByDate logpath2, LogFileAge

 

 

Sub DeleteFilesByDate(ByVal path, ByVal age)

' Deletes files over a certain age

Dim objFSO

Dim objFolder

Dim colFiles

Dim objFile

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(path)

Set colFiles = objFolder.Files

'WScript.Echo "More than " & age & " days old:"

For Each objFile In colFiles

' If file is over age then delete

If (objFile.DateCreated < Date - age) Then

'WScript.Echo "Deleting..."

'WScript.Echo objFile.Name & ": " & objFile.DateCreated

objFile.Delete

End If

Next

' Tidy up

Set objFSO = Nothing

Set objFolder = Nothing

Set colFiles = Nothing

End Sub

Posted

Correct me if I'm wrong, but it looks as if the OP was simply looking for a tweak to his original script that caters for folders as well as files. He was almost there, the only thing he needs to add to the end of his script to achieve what he's looking for is something along the lines of:

 

Set colSubFolds = folders.SubFolders
For Each objSubFold In colSubFolds
   If objSubFold.DateLastModified < dropdate Then
       objSubFold.Delete
   End If
Next

 

It will delete folders, and any content, if the last modified date is older than the dropdate. You might need to tweak around with the date comparison test (I've only briefly tested this, but it should work in the same manner as the files test you already listed). Hope this helps.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...