We have an application that is meant to create a folder with files in it daily, but when it creates todays folder, it's meant to delete previous days folders. However this isn't happening, so as a short term fix I'd like to write a script that will run daily, and delete all previous folders except the latest. I have found the below script online, which works perfectly for files - if I have 3 files each 1 minute apart, it will delete the 2 oldest, and keep the newest - however I'm having some problems getting it working for folders. Can anyone help me modify the script, so all subfolders of 'd:\temp' are deleted except the latest?
Const adVarChar = 200
Const adDate = 7
ipreserve=1 '# of files to preserve
sfolderpath="d:\temp" 'your path to the folder
if not fso.folderexists(sfolderpath) then
wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted."
set rs = createobject("ador.recordset")
for each ofile in ofolder.files
.addnew array("filepath","datelastmodified"), array(ofile.path,ofile.datelastmodified)
if not (rs.eof and rs.bof) then
do while not rs.eof
if icount>ipreserve then
on error resume next
on error goto 0