search
top

Search and Delete Files less than 1 hour old with vbscript

Ran into a situation where temporary created files were filling the drives of a Windows 2003 server. So I turned to vbscript and creating a scheduled task to keep the space issue at bay. Here is the script.

' VBscript to delete files older than 1 hour
' directory to delete
' cleanoldfiles.vbs

' change dir variable to directory to target files
sdir="d:\my dir"
dim dt, fso, odir, f
dt=now
set fso=createobject("scripting.filesystemobject")
if fso.folderexists(sdir) then
set odir=fso.getfolder(sdir)
for each f in odir.files
'"n" for minutes, "h" for hours etc, also change "zip" to file extension to look for
if (strcomp(fso.getextensionname(f.name),"zip",1)=0) and (datediff("n",f.datelastmodified,dt)>30) then
f.delete true 'forced
end if
next
set odir=nothing
else
wscript.echo "directory: " & sdir & " not found." & vbcrlf & "operation aborted."
end if
set fso=nothing

The script is pretty simple and can be changed to handles hours and minutes by changing the h and N variables. For under 1 hour times you need to make sure h = 0, this variable is set right after the file extention. Comments are listed in the code example above.

Hope this little script can be as much use for you as it is me.

2 Responses to “Search and Delete Files less than 1 hour old with vbscript”

  1. Bob says:

    Thanks for the script, was very useful to me

  2. joe says:

    Thank you! Thank you! Thank you! πŸ™‚ πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

top