cwhitmore
(Starting to like KiXtart)
2009-11-12 04:55 PM
Clean out directory after 2 days

I have the following script check \\server1\publicdr\scans for files over two days old and delete them. I've created a folder under \scans called ctx that I would also like to check. How can I use this script to check both folders w/o deleting the ctx folder, only the files inside of it?
 Code:
Break On
Call functions\timediff.udf
Call functions\dirplus.udf
$Dir = DirPlus("\\server1\publicdr\scans\","/a-d")
For Each $File In $Dir
  'Checking ' $File ' '
  If TimeDiff(GetFileTime($File),  'now', 'd') > 2
    "- File is more than 2 days old - deleting!" ?
    'Del ' $File ' /S' ?
    Del $File
  EndIf
Next
Exit 0


BradV
(Seasoned Scripter)
2009-11-13 11:56 AM
Re: Clean out directory after 2 days

Check if the file name is a directory.
 Code:
If Not GetFileAttr($File) & 16
   ; delete it
EndIf


Glenn BarnasAdministrator
(KiX Supporter)
2009-11-13 12:09 PM
Re: Clean out directory after 2 days

If there are other folders, and the CTX folder is the only one you don't want to delete, I'd do something like:
 Code:
$Files=DirList('root', 3)
For Each $File in $Files
  If Right($File, 4) <> 'CTX\'
    If TimeDiff(GetFileTime($File), 'now', 'd') > 2
      '- File is more than 2 days old - deleting!' ?
      'Del ' $File ' /S' ?
    EndIf
  EndIf
Next
Dirlist is faster than DirPlus in most situations, and you aren't using any of the extra DirPlus features, hence my choice to use it. It also returns the trailing "\" for folders, eliminating an additional call.

Change the value from 3 to 7 if you want to process subfolders, although I'd change the logic somewhat.. I'd search for files first, then make a second pass and only remove empty folders, excluding CTX\.

Glenn


cwhitmore
(Starting to like KiXtart)
2009-11-16 03:06 PM
Re: Clean out directory after 2 days

The only folder will be \CTX so I want to look in that folder and delete anything older than 2 days and then scan the original folder.