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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete filesif more than 5 files exist in Directory 1

Status
Not open for further replies.

beefstew

Technical User
Oct 27, 2002
124
GB
Hi

I need to create a script that will delete all files within a directory only if more than say 5 files exist

Any ideas please?

Many thanks
Beef
 
Any folder withind the root should be deleted (And all of its contents) but only if the folder was created 7 Days ago. It should only run if the are 5 or more folders within the root directory
 
Now I'm getting an idea what you need.

Have a look at this:
Code:
' ################################################################
' # cleanup-folder.vbs
' #
' # Removes all files older than 1 week only if more
' # than 4 files present in directory
' ################################################################

Dim fso, f, f1, fc, strComments, strScanDir

' user variables
' ----------------------------------------------------------------

strDir = "c:\test"
strDays = 7

' DO NOT EDIT BELOW THIS LINE
' (unless you know what you are doing)
'------------------------------------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strDir)

[b]If f.SubFolders.Count > 5 Then
    For Each sf In f.SubFolders
        If DateDiff("d", sf.DateCreated, Date) > strDays Then
            sf.Delete True
        End If
    Next
End If[/b]

Better?

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Argh! Forgot to close the code tag.. [blush]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
beefstew said:
Any folder withind the root should be deleted (And all of its contents) but only if the folder was created 7 Days ago. It should only run if the are 5 or more folders within the root directory

What if there are more than 5 subfolders and all of them are more than 7 days old? Do you want them all deleted, or should one or more be saved as backup? Perhaps delete the oldest subfolder(s) and keep the latest 5 versions?
 
Absolutely perfect!

MakeItSo, Thanks!!!!
 
Excellent.
Happy you got it sorted!
:)

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top