WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Deltree Examples

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Delete Files and Directories Based on Age


We have a small volume called "Common01" that everyone maps to and has full rights to. This is a free-for-all dumping ground for users to share data, with the understanding that anything over 35 days old gets deleted automatically. I don't recurse through the subdirectories because when the top subfolder gets to be 35 days old it is deleted and takes all its files with it.
;*******************************************************************************************************
;** Name:       CleanCommon
;** Author:     Marc Worrel, DTSEP
;** Version:    1.5    
;** Purpose:    Clean old files out of common directory
;** Date:       11/25/2002
;*******************************************************************************************************
;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set environmental information and initial variables   |
;-------------------------------------------------------- 
IntControl(12,1+4,0,0,0)          ;Suppress OK to close messages
IntControl (1003,0,0,0,0)         ;Keep WinBatch icon from being opened
IntControl (1008,1,0,0,0)         ;Enables Close command

;*******************************************************************************************************
;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; UDF's                                                 |
;-------------------------------------------------------- 
;+-----STYLE-----------------------+-----ICONS-----------------------+
;| OK               = 0            | STOP      = 16                  |
;| OKCANCEL         = 1            | QUESTION  = 32                  |    
;| ABORTRETRYIGNORE = 2            | EXCLAIM   = 48                  |
;| YESNOCANCEL      = 3            | INFO      = 64                  |   
;| YESNO            = 4            | WINICON   = 4096                |
;| RETRYCANCEL      = 5            |                                 |
;+-----BUTTONS---------------------+----RETURN VALUES----------------|
;| DEFAULTBUTTON1   = 0            | IDOK      = 1                   |
;| DEFAULTBUTTON2   = 256          | IDCANCEL  = 2                   |
;| DEFAULTBUTTON3   = 512          | IDABORT   = 3                   | 
;+---------------------------------| IDRETRY   = 4                   |
;| Combine flags with OR operator: | IDIGNORE  = 5                   |
;|      Flags=4|48|256|4096        | IDYES     = 6                   |
;|                                 | IDNO      = 7                   |
;+---------------------------------+---------------------------------+
#DefineFunction xMsgBox(Caption,Text,Flags)
	xMsg=DllCall(StrCat(DirWindows(1),"User32.dll"),long:"MessageBoxA",long:DllHwnd(""),lpstr:Text,lpstr:Caption,long:Flags)
	Return (xMsg)
#EndFunction

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Flags:
;FOF_ALLOWUNDO         = 64   ;Preserves undo information, if possible. 
;FOF_CONFIRMMOUSE      = 2    ;Not implemented. 
;FOF_FILESONLY         = 128  ;Performs the operation only on files if a wildcard filename (*.*) is specified. 
;FOF_MULTIDESTFILES    = 1    ;Indicates that the pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited. 
;FOF_NOCONFIRMATION    = 16   ;Responds with "yes to all" for any dialog box that is displayed. 
;FOF_NOCONFIRMMKDIR    = 512  ;Does not confirm the creation of a new directory if the operation requires one to be created. 
;FOF_RENAMEONCOLLISION = 8    ;Gives the file being operated on a new name (such as "Copy #1 of...") in a move, copy, or rename operation if a file of the target name already exists. 
;FOF_SILENT            = 4    ;Does not display a progress dialog box. 
;FOF_SIMPLEPROGRESS    = 256  ;Displays a progress dialog box, but does not show the filenames. 
;FOF_WANTMAPPINGHANDLE = 32   ;Fills in the hNameMappings member. The handle must be freed by using the 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction SHFileDelete (FileList,Flags)              ;MWorrel - 11/03
OpStruct=BinaryAlloc(32)
pFrom=BinaryAlloc (StrLen(FileList)+ItemCount(FileList,@TAB)+1)

For EachObj=1 To ItemCount (FileList,@TAB)
	ThisObj=ItemExtract (EachObj,FileList,@TAB)
	BinaryPokeStr (pFrom,BinaryEODGet(pFrom),ThisObj)
	BinaryEODSet (pFrom,BinaryEODGet(pFrom)+1)
Next 
BinaryEODSet (pFrom,BinaryEODGet(pFrom)+1)

BinaryPoke4 (OpStruct,4,3)
BinaryPoke4 (OpStruct,8,IntControl(42,pFrom,0,0,0))
BinaryPoke4 (OpStruct,16,Flags)

ShellOp=DllCall (StrCat(DirWindows(1),"Shell32.dll"),long:"SHFileOperationA",lpbinary:OpStruct)
;LastErr=DllCall (StrCat(DirWindows(1),"Kernel32.dll"),long:"GetLastError")
BinaryFree(OpStruct)
BinaryFree (pFrom)
Return (ShellOp)
#EndFunction

;*******************************************************************************************************
;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Body of script                                        |
;-------------------------------------------------------- 
IniFile=".\CleanCommon.ini"
If !FileExist (IniFile)
	xMsgBox ("Parameter File Not Found","Could not find CleanCommon.ini",16)
EndIf

Path=IniReadPvt ("Main","Path","",IniFile)
ProcessDirs=IniReadPvt ("Main","ProcessDirs","No",IniFile)
Confirm  =IniReadPvt ("Main","ConfirmDel","",IniFile)
SecLimit =IniReadPvt ("Limits","SecLimit","",IniFile)
MinLimit =IniReadPvt ("Limits","MinLimit","",IniFile)
HourLimit=IniReadPvt ("Limits","HourLimit","",IniFile)
DayLimit =IniReadPvt ("Limits","DayLimit","",IniFile)

;Determine how long object can exist before being flagged for deletion
TimeLimit=SecLimit
If MinLimit>0 Then TimeLimit =TimeLimit+(MinLimit*60)
If HourLimit>0 Then TimeLimit=TimeLimit+(HourLimit*3600)
If DayLimit>0 Then TimeLimit =TimeLimit+(DayLimit*86400)

Now=TimeYmdHms()                  ;Current time
ToDelete=""                       ;Initialize list of objects to be deleted
DirChange (Path)                  ;Change working directory to target path

If StrLower (ProcessDirs)<>"yes" Then Goto Files 
;Get directories to be deleted:
DirList=DirItemize (StrCat(Path,"*.*"))                    ;Get list of directories in target path
For EachDir=1 To ItemCount (DirList,@TAB)                  ;Determine how many items there are to check
	ThisDir=ItemExtract (EachDir,DirList,@TAB)              ;Get next directory to check
	DirCreated=FileTimeGetEx (StrCat(Path,ThisDir),1)       ;Check directory's creation date
	If TimeDiffSecs(Now,DirCreated)>=TimeLimit Then ToDelete=StrCat (ToDelete,Path,ThisDir,"\",@TAB);If too old, add to delete list
Next                                                       ;Return to top of loop

If ToDelete<>"" Then ToDelete=StrCat (ToDelete,@TAB)       ;If there are directories to delete, create blank line before files

:Files
;Get files to be deleted:
FileList=FileItemize (StrCat(Path,"*.*"))                  ;Get list of files in target path
For EachFile=1 To ItemCount (FileList,@TAB)                ;Determine how many items there are to check
	ThisFile=ItemExtract (EachFile,FileList,@TAB)           ;Get next file to check
	FileCreated=FileTimeGetEx (StrCat(Path,ThisFile),1)     ;Check file's creation date
	If TimeDiffSecs(Now,FileCreated)>=TimeLimit Then ToDelete=StrCat (ToDelete,Path,ThisFile,@TAB)  ;If too old, add to delete list
Next                                                       ;Return to top of loop

If ToDelete=="" Then Exit                                  ;If there's nothing to delete, quit

;Confirm list of objects to delete
If Confirm<>"No"
	CfmMsg=StrCat("Delete these directories and/or files?",@CRLF,@CRLF,StrReplace(ToDelete,@TAB,@CRLF))  ;Message to display in Confirm box
	If xMsgBox ("Confirm Deletion",CfmMsg,4|32)<>6 Then Exit     ;Ask if list of objects should be deleted, exit if Yes is not clicked
EndIf

For EachDel=1 To ItemCount (ToDelete,@TAB)                 ;Determine how many items there are to delete
	ThisDel=ItemExtract (EachDel,ToDelete,@TAB)             ;Get next item to delete
	If ThisDel=="" Then Next                                ;If blank line, skip
	ThisDelLen=StrLen (ThisDel)                             ;Get length of item's name
	If StrIndex (ThisDel,"\",0,@BACKSCAN)==ThisDelLen Then ThisDel=StrFixChars (ThisDel,"",ThisDelLen-1)   ;Strip trailing "\"'s
	Delete=SHFileDelete (ThisDel,16)                        ;Delete item
;	Message (ThisDel,Delete)
Next                                                       ;Return to top of loop

Exit

CLEANCOMMON.INI

[Main]
Path        = \\nwmnepha01_common01_server\common01\common\
ProcessDirs = Yes
ConfirmDel  = Yes

[Limits]
SecLimit    = 0
MinLimit    = 0
HourLimit   = 0
DayLimit    = 35 

Article ID:   W16211
File Created: 2004:03:30:15:43:16
Last Updated: 2004:03:30:15:43:16