Jump to content

delete files older than x days


Recommended Posts

Hello to all,

try to script from myself script that delete files older than x days.

Need because can't find DOS syntax suitable for all cases or existing tool.

So i think to autoit to build command tool with simple syntax.

My idea is to delete or preserve files in a given date range, syntax idea is :

tool *.jpg /delete > 2009.05.21
tool *.jpg /preserve > 2009.05.21

use data in ISO format yyyymmdd is needed because date format is not = for all.

Think to use operator (> < => =< <> ) to help user to batch in logic way

Have some problem with datediff, and other problem, any skilled scripter can help me to build this ?

(also with logic and analysis)

thank you for reading,

m.

Link to comment
Share on other sites

What have you got so far? Are you already familiar with $CmdLine and how to parse arguments from it?

:)

P.S. If meant to find someone to write it for you, see the Rent-A-Code link in my sig.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

PsaltyDS thank you for reply.

like idea so search people interested.

Try to write myself, but like have some skilled people around to make nice script.

Have some problem with date format. That's all.

Not search for rent, next time, maybe :)

See u,

m.

Link to comment
Share on other sites

Do my best, anyone can correct it ?

#include <file.au3>
#include <array.au3>

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <Date.au3>
#include <WindowsConstants.au3>

Global $iMemo

Dim $sourcedir
Dim $szDrive, $szDir, $szFName, $szExt
Dim $new_file, $my_count, $search_source_file, $split_source_Path, $file, $switch_from
dim $f_operator = 0

If $CmdLine[0] <> 3 Then
    ConsoleWrite(@CRLF)
    ConsoleWrite(@ScriptName & " - delete file with Modified date comparison" & @CRLF)
    ConsoleWrite(@CRLF)
    ConsoleWrite("Delete files where date is :" & @CRLF)
    ConsoleWrite(@CRLF)
    ConsoleWrite("[a =  b] Equal to given date" & @CRLF)
    ConsoleWrite("[a <> b] Not equal to given date " & @CRLF)
    ConsoleWrite("[a >  b] Greater than given date" & @CRLF)
    ConsoleWrite("[a >= b] Greater than or equal to given date" & @CRLF)
    ConsoleWrite("[a <  b] Less than given date" & @CRLF)
    ConsoleWrite("[a <= b] Less than or equal to given date" & @CRLF)
    ConsoleWrite(@CRLF)
    ConsoleWrite("Use always '" & chr(34) & "' to avoid DOS issue. Syntax :" & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "[operator]" & chr(34) & " " & "[yyyy/mm/dd]" & @CRLF)
    ConsoleWrite(@CRLF)
    ConsoleWrite("Some example :" & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "=" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "<>" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & ">" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & ">=" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "<" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "<=" & chr(34) & " " & _NowDate() & @CRLF)
    ConsoleWrite(@CRLF)
    
    
    
;//////////////////////////////////////////////////////////////////
;//      Menage Switches
;//////////////////////////////////////////////////////////////////
ElseIf $CmdLine[0] = 3 Then
;//////////////////////////////////////////////////////////////////
;//        VARIABLES
;//////////////////////////////////////////////////////////////////
    $full_path = $CmdLine[1]
    $operator = $CmdLine[2]
    $user_date = $CmdLine[3]
    
;if user do not specify PATH, then user means actual path
    if stringleft($full_path, 2) = "*." then $full_path = @ScriptDir & "\" & $full_path
        
;apply flag to assign choosen user operator
    if $operator = "=" then $f_operator = 1
    if $operator = "<>" then $f_operator = 2
    if $operator = ">" then $f_operator = 3
    if $operator = ">=" then $f_operator = 4
    if $operator = "<" then $f_operator = 5
    if $operator = "<=" then $f_operator = 6

    if $f_operator = 0 then;if not change in flag retrun error
        ConsoleWrite(@CRLF)
        ConsoleWrite("Bad syntax, try :" & @CRLF)
        ConsoleWrite(@ScriptName & " c:\temp\*.* " & chr(34) & "<>" & chr(34) & " " & _NowDate() & @CRLF)
        ConsoleWrite("Delete all files with date Not equal to " & _NowDate() & @CRLF)
        ConsoleWrite(@CRLF)
        Exit
    EndIf
    

;//////////////////////////////////////////////////////////////////
;//       SPLIT PATH FOR EASY USE (-p)
;//////////////////////////////////////////////////////////////////
    $split_source_Path = _PathSplit($full_path, $szDrive, $szDir, $szFName, $szExt)
;~  _ArrayDisplay($split_source_Path,"Demo _PathSplit()")
    $file_filter = $split_source_Path[3] & $split_source_Path[4]
    $sourceFolder = $split_source_Path[1] & $split_source_Path[2]
    
    
;Gather files into an array
    $fileList = _FileListToArray($sourceFolder, $file_filter, 1)
;~  _ArrayDisplay($fileList,"Demo _PathSplit()")

;Loop through array
    For $X = 1 to $fileList[0]
    

    ;Retrieve creation time of file
;~      0 = Modified (default)
;~      1 = Created
;~      2 = Accessed
;~    FileGetTime ( "filename" [, option [, format]] )

        $file_Date = FileGetTime($sourceFolder & $fileList[$X], 0, 0)
        $fDate = StringFormat("%s/%s/%s", $file_Date[0],$file_Date[1],$file_Date[2])
        

        if $f_operator = 1 then; =
            if $fDate = $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date = " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        elseif $f_operator = 2 then; <>
            if $fDate <> $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date <> " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        elseif $f_operator = 3 then; >
            if $fDate > $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date > " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        elseif $f_operator = 4 then; >=
            if $fDate >= $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date >= " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        elseif $f_operator = 5 then; <
            if $fDate < $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date < " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        elseif $f_operator = 6 then; <=
            if $fDate <= $user_date then 
                ConsoleWrite("Find " & $fileList[$X] & " with date <= " & $user_date & @CRLF)
                FileDelete($sourceFolder & $fileList[$X])
            endif
        Else
            ConsoleWrite("Can't find anything that match with given filter..." & @CRLF)
        EndIf

    Next
EndIf

If qant to try remember to compile :

Aut2exe.exe" /in della.au3 /out della.exe /console /unicode

thank you for any suggestion,

m.

Link to comment
Share on other sites

Here's a little example that works for me ;o

Source :

#Include <File.Au3>
#Include <String.Au3>
$Drive = @ScriptDir 
$Type = '.jpg'
$List = _FileListToArray ($Drive)   
$Current_Year = @YEAR
$Current_Month = @MON
$Current_Day = @MDAY
$Days_Old = '0'
$Check = $Current_Day - $Days_Old
If $Check = '0' Or StringInStr ($Check, '-') <> '0' Then Exit
    
    
For $A = '1' To $List['0']
If StringInStr (_FileGetType ($List[$A]), $Type) <> '0' Then 
$Current = $Drive & '\' & $List[$A]
$Time = FileGetTime ($Current)
$Write_Year = $Time['0']
$Write_Month = $Time['1']
$Write_Day = $Time['2']
If $Write_Year = $Current_Year And $Write_Month = $Current_Month Then 
; File was written this month.
If $Write_Day <= $Current_Day - $Days_Old Then 
; File is too old, delete it.
ConsoleWrite ('Too old : ' & $List[$A] & @CRLF)
EndIf 
Else 
; File is a year+ old, its too old, delete it.
ConsoleWrite ('Too old ' & $List[$A] & @CRLF)
EndIf 
EndIf 
Next

Func _FileGetType ($iFile)
$String = _StringReverse ($iFile)
$String = StringSplit ($String, '.')
$String = '.' & _StringReverse ($String['1'])
Return $String
EndFunc

Hope it helps! :)

- John

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...