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.

FileMove FileDelete FileCopy Based on Date


;*******************************************************************************************************
;** Name:       FileSieve
;** Author:     Marc Worrel, DTSEP
;** Version:    2.0    
;** Purpose:    Delete files/folders from dir structure based on age
;** Date:       11/25/2002
;*******************************************************************************************************
;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set environmental information and initial variables   |
;-------------------------------------------------------- 
IntControl (5,1,0,0,0)            ;System & Hidden files or directories are seen and used
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

#DefineFunction DirItemRecursive (Tree,Dir,Mask,CurrentLevel,MaxLevel)
	If CurrentLevel>=MaxLevel Then Return (Tree)
	CurrentLevel=CurrentLevel+1
	
	DirChange (Dir)
	If Tree=="" Then Then Tree=DirGet()
	Else Tree=StrCat (Tree,@TAB,DirGet())
	DList=ItemSort (DirItemize("*.*"),@TAB)
	DCount=ItemCount (DList,@TAB)
	For xx=1 To DCount
	   ThisDir=ItemExtract (xx,DList,@TAB)
	   Tree=DirItemRecursive (Tree,ThisDir,Mask,Currentlevel,MaxLevel)
	Next
	
	DirChange ("..")
	Return (Tree)
#EndFunction

#DefineFunction FileItemRecursive (Tree,Dir,Mask,CurrentLevel,MaxLevel)
	If CurrentLevel>=MaxLevel Then Return (Tree)
	CurrentLevel=CurrentLevel+1
	
	DirChange (Dir)
	DList=ItemSort (DirItemize("*.*"),@TAB)
	DCount=ItemCount (DList,@TAB)
	For xx=1 To DCount
	   ThisDir=ItemExtract (xx,DList,@TAB)
	   Tree=FileItemRecursive (Tree,ThisDir,Mask,CurrentLevel,MaxLevel)
	Next
	If Tree=="" Then tt=""
	Else tt=@TAB
	FList=FileItemPath (Mask)
	If FList != ""
	   FList=ItemSort (FList,@TAB)
	   Tree=StrCat (Tree,tt,FList)
	EndIf
	
	DirChange("..")
	Return (Tree)
#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=".\FileSieve.ini"
If !FileExist (IniFile)
	xMsgBox ("Parameter File Not Found","Could not find FileSieve.ini.  Program will now exit",16)
	Exit
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
DirList=DirItemRecursive ("",Path,"*.*",0,9999)            ;Get list of subdirectories in target directory structure
If ItemLocate (Path,DirList,@TAB) Then DirList=ItemRemove (ItemLocate(Path,DirList,@TAB),DirList,@TAB)  ;Don't delete root folder
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 (ThisDir,1)                    ;Check directory's creation date
	If TimeDiffSecs(Now,DirCreated)>=TimeLimit Then ToDelete=StrCat (ToDelete,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
FileList=FileItemRecursive("",Path,"*.*",0,9999)           ;Get list of files in target directory structure
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 (ThisFile,1)                  ;Check file's creation date
	If TimeDiffSecs(Now,FileCreated)>=TimeLimit Then ToDelete=StrCat (ToDelete,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
	If (!FileExist (ThisDel) && !DirExist (ThisDel)) Then Next   ;If item has already been deleted (because it's parent was), 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)                                 ;Show result of each item being deleted.
Next                                                       ;Return to top of loop

Exit

FILESIEVE.INI
[Main]
Path        = \\Server\share
ProcessDirs = Yes
ConfirmDel  = Yes

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

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