Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting files/folders over 7 days old ?

Status
Not open for further replies.

ACraib

MIS
Sep 7, 2001
579
GB
Hi

Does anybody have a vbs file that will look at all files / folders within a specified folder and then delete any that are over 7 days old?

cheers

Andrew

 
Set refWMIService = GetObject("winMgmts:")
Set colSmallFiles = refWMIService.ExecQuery(&quot;SELECT * FROM CIM_DataFile WHERE FileSize < '1024'&quot;)

the above it using WMI with a WQL query (similar to SQL i guess)

This would return a standard 'collection' of files smaller than 1024. you could change this to be ones where the date is more/less than Now() - 7 days

cant remember if the CIM_DataFile class applies to folders as well. if not there will be a folder class.

word or warning, if you run this on a system with lots of files/folders it will take a while. try and change the query to reflect the right driver letter or root foler for instance

you could do the same thing using a FSO objects.
if you are only interested in a few folders that might be better. try using
Set objRootFld = FSO.GetFolder(&quot;d:\test&quot;)
'Then something like
For Each aFolder in objRootFld.SubFolders

strX = aFolder.Name
msgbox strX
msgbox aFolder.DateLastAccessed
Set subFolder = FSO.GetFolder(strX)

For Each aFile In subFolder.Files
msgbox aFile.DateLastAccessed
Next
Next

 
Hi Mrmovie

Thanks for that ... but I should have explained I'm not a programmer at all. :( I was hoping that someone could post a completed script I could just cut and paste out and then run using W2Ks task scheduler.

Thanks for your response though.

Andrew
 
see the below, because you have not been specific about what folders/drives etc you want to check this script/query will check for all available files, including mapped drives.

it works fine on my machine but as you are searching all files it takes a while!

if you decided to cancel the query, end the wscipt.exe process and also stop and then restart the WMI service on the machine running it.
all i have done is display the name and details of a file over 7 days old. you can do aFile.Delete if you want to. i would use with caution as this could delete system files etc, hence the need for you to decided where it is you want to search, this could narrow the search and make it quicker of you could do something like
If Left(aFile.Name,8) = &quot;d:\test\&quot; Then
aFile.Delete
End If

the above would go in the For/Next loop

regards,
Richard



Set refWMIService = GetObject(&quot;winMgmts:&quot;)
'datA = Now() - 7
'msgbox datA
strDate = Year(DateAdd(&quot;d&quot;,-7,Now)) & AddZero(Month(DateAdd(&quot;d&quot;,-7,Now))) & AddZero(Day(DateAdd(&quot;d&quot;,-7,Now))) & AddZero(Hour(DateAdd(&quot;d&quot;,-7,Now))) & AddZero(Minute(DateAdd(&quot;d&quot;,-7,Now))) & AddZero(Second(DateAdd(&quot;d&quot;,-7,Now))) & &quot;.000000&quot; & &quot;+000&quot;
strQ = &quot;SELECT * FROM CIM_DataFile WHERE LastAccessed < '&quot; & strDate & &quot;'&quot;
'msgbox strQ
'msgbox DateAdd(&quot;d&quot;,-7,Now)


Set colRarelyUsedFiles = refWMIService.ExecQuery(strQ)
For Each aFile in colRarelyUsedFiles
msgbox aFile.LastModified
msgbox aFile.Name
Next




Function AddZero(pNum)
If pNum <= 9 Then
AddZero = &quot;0&quot; & pNum
Else AddZero = pNum
End If
End Function
msgbox &quot;end&quot;
 
This script will not delete folders over 7 days old (not sure why you would want to do this in the first place), but will delete folders that are empty. This includes all subfolders and files.

' folder to start search in...
path = &quot;c:\tmp&quot;

' delete files older than 7 days...
killdate = date() - 7

arFiles = Array()
set fso = createobject(&quot;scripting.filesystemobject&quot;)

' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true

nDeleted = 0
for n = 0 to ubound(arFiles)
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
on error resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number <> 0 then
wscript.echo &quot;Unable to delete: &quot; & arFiles(n).path
else
nDeleted = nDeleted + 1
end if
on error goto 0
next

msgbox nDeleted & &quot; of &quot; & ubound(arFiles)+1 _
& &quot; eligible files were deleted&quot;


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
on error resume next
'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files

for each file in files
' uses error trapping around access to the
' Date property just to be safe
'
dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodified
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
next

if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub

'The DeleteEmptyFolders sub below will remove all empty folders (also the
'complete folder/subfolder/subfolder/... structure will be removed if no files
'exists in it).

'Note the second parameter to the sub (bDeleteThisFolder). If it is set to True,
'the script will remove the root folder in the input path as well if it is empty
'(the folder &quot;c:\tmp&quot; itself in the example below) .

' folder to start search in...
Path = &quot;c:\tmp&quot;

DeleteEmptyFolders Path, False

sub DeleteEmptyFolders(sPath, bDeleteThisFolder)

set folder = fso.getfolder(sPath)

'recurse first...
'
for each fldr in folder.subfolders
DeleteEmptyFolders fldr.path,true
next

'if no files or folders then delete...
'
'bDeleteThisFolder is false for
'the root of the subtree, and true for
'sub-folders (unless you want to delete
'the entire subtree if it is empty).
'
if (folder.files.count = 0) and _
(folder.subfolders.count) = 0 and _
bDeleteThisFolder then
on error resume next
folder.delete
exit sub
end if

end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top