Solved

Script to delete all subfolders except newest?

Posted on 2012-03-30
3
723 Views
Last Modified: 2012-06-27
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

set fso=createobject("scripting.filesystemobject")
if not fso.folderexists(sfolderpath) then
    set fso=nothing
    wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted."
    wscript.quit
end if

set ofolder=fso.getfolder(sfolderpath)
set rs = createobject("ador.recordset")
with rs.fields
    .append "filepath",adVarChar,255
    .append "datelastmodified",adDate
end with

with rs
    .open
    for each ofile in ofolder.files
        .addnew array("filepath","datelastmodified"), array(ofile.path,ofile.datelastmodified)
        .update
    next
end with

icount=0
if not (rs.eof and rs.bof) then
    rs.sort="datelastmodified desc"
    rs.movefirst
    do while not rs.eof
        icount=icount+1
        if icount>ipreserve then
            on error resume next
            fso.deletefile rs.fields("filepath"),true
            on error goto 0
        end if
        rs.movenext
    loop
end if

set rs=nothing
set ofolder=nothing
set fso=nothing

Open in new window

0
Comment
Question by:bjblackmore
3 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
Comment Utility
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
Comment Utility
Try the following..

Const adVarChar = 200
Const adDate = 7

ipreserve=1    '# of files to preserve
sfolderpath="d:\temp"    'your path to the folder

set fso=createobject("scripting.filesystemobject")
if not fso.folderexists(sfolderpath) then
    set fso=nothing
    wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted."
    wscript.quit
end if

set ofolder=fso.getfolder(sfolderpath)
set rs = createobject("ador.recordset")
with rs.fields
    .append "subfolderpath",adVarChar,255
    .append "datelastmodified",adDate
end with

with rs
    .open
    for each osf in ofolder.subfolders
        .addnew array("subfolderpath","datelastmodified"), array(osf.path,osf.datelastmodified)
        .update
    next
end with

icount=0
if not (rs.eof and rs.bof) then
    rs.sort="datelastmodified desc"
    rs.movefirst
    do while not rs.eof
        icount=icount+1
        if icount>ipreserve then
            on error resume next
            fso.deletefolder rs.fields("subfolderpath"),true
            on error goto 0
        end if
        rs.movenext
    loop
end if

set rs=nothing
set ofolder=nothing
set fso=nothing
                                  

Open in new window

0
 

Author Closing Comment

by:bjblackmore
Comment Utility
Works perfectly!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now