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
 
Where exactly are you stuck?

“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.
 
I have a script that will delete all files in a directory but I only want it to run if there are more than 5 files in the directory

psuedo if exist more than 5 files


' ################################################################
' # cleanup-folder.vbs
' #
' # Removes all files older than 1 week
' # Authored by Spencer Kuziw (s.kuziw-at-epic.ca)
' # Based on code by YellowShoe
' # Version 1.0 - Sept 23 2008
' ################################################################

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

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

strDir = "c:\CAP"
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)
Set fc = f.Files

For Each f1 in fc
If DateDiff("d", f1.DateCreated, Date) > strDays Then
'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
fso.DeleteFile(f1)
End If
Next

'wscript.echo strComments

WScript.Quit
' eof
 
beefstew said:
psuedo if exist more than 5 files
Non-pseudo looks like this:
If f.Files.Count > 5 Then

:)

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.
 
Could you please let me know whereabouts in the script this would have to sit

Thanks
 
Code:
Set f = fso.GetFolder(strDir)
Set fc = f.Files
[b]If f.Files.Count > 5 Then[/b]
  For Each f1 in fc
    If DateDiff("d", f1.DateCreated, Date) > strDays Then
      'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
      fso.DeleteFile(f1)
    End If
  Next
[b]End If[/b]

“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.
 
Thanks for that

One last thing if you may; if I wanted to do this for folders instead of files what would need to be changed?

Thanks
 
If you wanted to delete the entire folder, I'd move the count check a little upward:
Code:
Set f = fso.GetFolder(strDir)
If f.Files.Count > 5 Then
  f.Delete True
'alternatively: fso.deletefolder f.Path
Else
  Set fc = f.Files
  For Each f1 in fc
    If DateDiff("d", f1.DateCreated, Date) > strDays Then
      'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
      fso.DeleteFile(f1)
    End If
  Next
End If

That what you need?

“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.
 
Sorry but no that just deleted a load of files. Thanks heavens that wasn't live. lol

What I would like it to do is look in a directory and delete all sub folders over 7 days old, but only if there are more than 5 sub folders in total

Basically to stop backups taking up all space but at the same time ensuring all backup files will not be deleted if the backup does not run for a few Days for some reason

Each backup creates its own folder

Thanks
 
/root/folder1
/root/folder2
/root/folder3
/root/folder4

So I would like to monitor Root. If folder 123 and 4 are older than 7 Days delete folders 123 and 4 but only if there are 5 folders. In this case no folders would be deleted because there are only 4 folders
 
I'm interested in folders and not the files within them as the Cognos application creates a folders structure for each backup
 
beefstew said:
delete all sub folders over 7 days old, but only if there are more than 5 sub folders in total
That is why it's always a good choice to be precise rather than vague when asking for help.
:)
Code:
Set f = fso.GetFolder(strDir)
  Set fc = f.Files
  For Each f1 in fc
    If DateDiff("d", f1.DateCreated, Date) > strDays[b][COLOR=#CC0000] And f.Files.Count > 5[/color][/b] Then
      'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
      [b][COLOR=#CC0000]fso.DeleteFolder(f.Path)
      Exit For[/color][/b]
    End If
  Next

That closer to what you need?
;-)

“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.
 
I want to delete going by the created date of the actual folder
 
Wait, wait!!
more than 5 sub folders in total
only want it to run if there are more than 5 files in the directory
Now WHAT exactly are the criteria?

Scenario:
folder/subfolder/[n]subfolders with n files each

do you wish to delete this folder:
folder/subfolder(older than date)/[>5]subfolders
??

Please state precisely what you have and what exactly you wish to delete at what level.
And make sure not to mix up "file" with "folder"

“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.
 
You wanted something like this ?
Code:
Set f = fso.GetFolder(strDir)
Set fc = f.SubFolders
If fc.Count > 5 Then
  For Each f1 in fc
    If DateDiff("d", f1.DateCreated, Date) > strDays Then fl.Delete
  Next
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is what I have but it doesn't seem to work:

' ################################################################
' # 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)
Set fc = f.Files
For Each f1 in fc
If DateDiff("d", f1.DateCreated, Date) > strDays And f.Files.Count > 5 Then
'strComments = strComments & f1.name & " " & f1.DateCreated & vbCrLf
fso.DeleteFolder(f.Path)
Exit For
End If
Next

'wscript.echo strComments

WScript.Quit
' eof
 
there are 6 folders in the test Directory and all folders and the created date is Month's old
 
beefstew said:
This is what I have but it doesn't seem to work:

Again: Please state precisely what you have and what exactly you wish to delete at what level under which criteria.

“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