Page 1 of 1 1
Topic Options
#196672 - 2009-11-12 04:55 PM Clean out directory after 2 days
cwhitmore Offline
Starting to like KiXtart

Registered: 2001-05-23
Posts: 102
Loc: Austin, Texas, USA
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


Edited by Mart (2009-11-15 09:52 PM)
Edit Reason: Please use code tags when posting code.

Top
#196686 - 2009-11-13 11:56 AM Re: Clean out directory after 2 days [Re: cwhitmore]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Check if the file name is a directory.
 Code:
If Not GetFileAttr($File) & 16
   ; delete it
EndIf

Top
#196687 - 2009-11-13 12:09 PM Re: Clean out directory after 2 days [Re: cwhitmore]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196712 - 2009-11-16 03:06 PM Re: Clean out directory after 2 days [Re: Glenn Barnas]
cwhitmore Offline
Starting to like KiXtart

Registered: 2001-05-23
Posts: 102
Loc: Austin, Texas, USA
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.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.062 seconds in which 0.028 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org