Sign in to follow this  
Followers 0
manish.aggarwal

Delete Files

7 posts in this topic

can any one help me with an example script for deleting files from windows drive whichever is older then a month. :(

Thx in advance

-Maish

Share this post


Link to post
Share on other sites



can any one help me with an example script for deleting files from windows drive whichever is older then a month. :(

Thx in advance

-Maish

<{POST_SNAPBACK}>

from a whole drive or just a folder?

Share this post


Link to post
Share on other sites

with in a folder

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Okay ive been playing a bit with it... this is what i have made so far

Im not the best coder to calculate all that shit, but this is what i could figure out.. its not deleting anything, but its shows what to do with the file it have found, just to show how to make it.. sort of

#Include <GUIConstants.au3>

$Year = @Year
$Month = @MON
$Day = @MDAY

GUICreate("Delete Files",250,250)

$DirBrowse = GUICtrlCreateInput("",10,30,150,20)
GUICtrlsetstate(-1,$GUI_DISABLE)
$Button = GUICtrlCreateButton("Browse",170,29,70,22)
GUICtrlCreatelabel("Older than 'YYYY/MM/DD'",10,60,150,20)
GUICtrlSetfont(-1,8,600)
$YInput = GUICtrlCreatecombo("",10,80,50,20)
$MInput = GUICtrlCreatecombo("",65,80,40,20)
$DInput = GUICtrlCreatecombo("",110,80,40,20)
GUICtrlSetdata($YInput,"2000|2001|2002|2003|2004|2005",$Year)
GUICtrlSetdata($MInput,"01|02|03|04|05|06|07|08|09|10|11|12",$Month)
GUICtrlSetdata($DInput,"01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31",$Day)

$DeleteFiles = GUICtrlCreateLabel("",10,150,200,20)

GUISetState()
While 1
    $msg = GUiGetmsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
        
    If $msg = $Button Then
    $Dir = FileSelectFolder("","","","")
    GUICtrlSetdata($DirBrowse,$Dir)
    _Search($Dir & "*.*")
    EndIf
WEnd    

Func _Search($DirToSearch)
$search = FileFindFirstFile($DirToSearch)  
If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern")

$X = 0
While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    $t = FileGetTime($Dir & $file, 1)
    $yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2]
    $attrib = FileGetAttrib($Dir & $file)
    If @error Then MsgBox(4096,"Error", "Could not obtain attributes.")
    
    If StringInStr($attrib, "D") Then
        Else    
            $X = $X +1
            If $t[0] = GUICtrlRead($YInput) AND $t[1] = GUICtrlRead($MInput) AND $t[2] = GUICtrlRead($DInput) Then
                msgbox(0,"","Same date, do not delete")
                GUICtrlSetdata($DeleteFiles,$X & " Files found")
            
            ElseIf $t[0] < GUICtrlRead($YInput) OR $t[1] < GUICtrlRead($MInput) OR $t[2] < GUICtrlRead($DInput) Then
            If $t[0] < GUICtrlRead($YInput) Then 
                msgbox(0,"","Delete year - " & $Dir & $file &" ("& $yyyymd &")")
                GUICtrlSetdata($DeleteFiles,$X & " Files found")
            ElseIf $t[0] <= GUICtrlRead($YInput) AND $t[1] < GUICtrlRead($MInput)Then
                msgbox(0,"","Delete Month - " & $Dir & $file &" ("& $yyyymd &")")
                GUICtrlSetdata($DeleteFiles,$X & " Files found")
            ElseIf $t[0] <= GUICtrlRead($YInput) AND $t[1] <= GUICtrlRead($MInput) AND $t[2] < GUICtrlRead($DInput) Then
                msgbox(0,"","Delete Day - " & $Dir & $file &" ("& $yyyymd &")")
                GUICtrlSetdata($DeleteFiles,$X & " Files found")
            EndIf
        EndIf
    EndIf
WEnd
FileClose($search)
EndFunc

Share this post


Link to post
Share on other sites

Oh dear... how ugly... Do you see that FileFindNextFile($search) is executed even if the research is unsucessiful?

Why making so complex script for a simple search made with FileFindFirst / FileFindNext file + a time difference calculated with the date.au3 function?

Share this post


Link to post
Share on other sites

Oh dear... how ugly... Do you see that  FileFindNextFile($search) is executed even if the research is unsucessiful?

Why making so complex script for a simple search made with FileFindFirst / FileFindNext file + a time difference calculated with the date.au3 function?

<{POST_SNAPBACK}>

Don't call my script ugly :( I said im "new" with this type of caluculating... i donøt even though of date.au3.. so exscuse me for being noob :(

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Apologies accepted Wb. But remember, being a noob is not a fault, giving bad peices of advice is.

#include <Date.au3>
Func _DeleteOlder($sDir, $iDays)
  ;This function returns the number of files deleted or it sets error to 1 if the folder can't be found.
   If StringLeft($sDir,1) <> '\' Then $sDir = $sDir & '\'
   
   Local $iO = 0, $h, $sFileN, $vFileTime, $sNowTime = _NowCalc()
   $h = FileFindFirstFile($sDir & '*')
   
   If $h <> -1 Then
      While 1
         $sFileN = FileFindNextFile($h)
         If @error Then
            FileClose($h)
            ExitLoop
         EndIf
         If StringInStr(FileGetAttrib($sDir & $sFileN),'d') Then ContinueLoop
         $vFileTime = FileGetTime($sDir & $sFileN)
         $vFileTime = $vFileTime[0] & '/' & $vFileTime[1] & '/' & $vFileTime[2] & ' ' & $vFileTime[3] & ':' & $vFileTime[4] & ':' & $vFileTime[5]
         
         If _DateDiff('d', $vFileTime, $sNowTime) > $iDays Then
            FileDelete($sDir & $sFileN)
            $iO = $iO + 1
         EndIf
         
      WEnd
   Else
      SetError(1)
   EndIf
   Return $iO
EndFunc
Edited by ezzetabi

Share this post


Link to post
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
Sign in to follow this  
Followers 0