Set refWMIService = GetObject("winMgmts:"
Set colSmallFiles = refWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE FileSize < '1024'"
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("d:\test"
'Then something like
For Each aFolder in objRootFld.SubFolders
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.
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) = "d:\test\" Then
aFile.Delete
End If
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 = "c:\tmp"
arFiles = Array()
set fso = createobject("scripting.filesystemobject"
' 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.delete true
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles.path
else
nDeleted = nDeleted + 1
end if
on error goto 0
next
msgbox nDeleted & " of " & ubound(arFiles)+1 _
& " eligible files were deleted"
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 "c:\tmp" itself in the example below) .
' folder to start search in...
Path = "c:\tmp"
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.