Jump to content

directory create time


placebo
 Share

Recommended Posts

you are right! filegettime() does the trick!

i'm trying to make a script that checks all the files and directory's in a specific directory. I want the script to delete all the files/directory's which are older than 1 week.

My scripts works for the directory and the files in the "root" of my directory I want to clean up. It doesn't work for the files in een subdirectory. Is there an easy way to do this?

here is my code (it's quick and dirty and i'm still learning):

#include <Date.au3>

;ini uitlezen
$var = IniRead("config.ini", "algemeen", "directory_to_clean", "NotFound")
$log = IniRead("config.ini", "algemeen", "log_directory", "d:\")
$age = IniRead("config.ini", "algemeen", "age", "1")
$agetype = IniRead("config.ini", "algemeen", "agetype", "w")


;directory_naam bepalen
$directory_naam = StringReplace(_DateTimeFormat( _NowCalc(),2), "/", "-")
$log_file_name = $log & "\" & $directory_naam & ".txt"
DirCreate($log)

; Shows the filenames of all files in the current directory.
$files_directory = FileFindFirstFile($var & "\*.*")  


; Check if the search was successful
If $files_directory = -1 Then
; MsgBox(0, "Error", "geen bestanden gevonden")
    FileWriteLine($log_file_name, "error: geen bestanden gevonden")
    Exit
EndIf

While 1
    $file = FileFindNextFile($files_directory) 
    If @error Then ExitLoop
    
; MsgBox(4096, "File:", $file)

    $time = FileGetTime($var &"\"& $file)
;msgbox(0, "", $time[0] &"/"& $time[1] &"/" & $time[2])
;msgbox(0, "info", _DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY))
    If (_DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY) > $age) then 
        FileWriteLine($log_file_name, "DELETE: " & $file & "                  age: " & _DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY))
        FileDelete ($var & "\" & $file)
        DirRemove ($var & "\" & $file, 1)
    EndIf
WEnd

FileWriteLine($log_file_name, "")

;Close the search handle
FileClose($files_directory)
Edited by filipvds
Link to comment
Share on other sites

#include <Date.au3>

;ini uitlezen
$var = IniRead("config.ini", "algemeen", "directory_to_clean", "NotFound")
$log = IniRead("config.ini", "algemeen", "log_directory", "d:\")
$age = IniRead("config.ini", "algemeen", "age", "1")
$agetype = IniRead("config.ini", "algemeen", "agetype", "w")

;~ $var = "d:\test"
;~ $log = "d:\test\log"
;~ $age = "1"
;~ $agetype = "w"


;directory_naam bepalen
$directory_naam = StringReplace(_DateTimeFormat( _NowCalc(),2), "/", "-")
$log_file_name = $log & "\" & $directory_naam & ".txt"
DirCreate($log)

; Shows the filenames of all files in the current directory.
If StringRight( $var, 1 ) = "\" Then $var = StringTrimRight( $var, 1 )
$files_array = _FileSearch($var & "\*.*", 1, 0, 1, 0) ; Only grabbing files recursively

; Check if the search was successful
If $files_array[0] = 0 Then
; MsgBox(0, "Error", "geen bestanden gevonden")
    FileWriteLine($log_file_name, "error: geen bestanden gevonden")
    Exit
EndIf

For $file = 1 To $files_array[0]
; MsgBox(4096, "File:", $file)
    $time = FileGetTime($files_array[$file])
;msgbox(0, "", $time[0] &"/"& $time[1] &"/" & $time[2])
;msgbox(0, "info", _DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY))
    If (_DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY) > $age) then
        FileWriteLine($log_file_name, "DELETE: " & $files_array[$file] & "                  age: " & _DateDiff($agetype, $time[0] &"/"& $time[1] &"/" & $time[2], @YEAR &"/" & @MON &"/" & @MDAY) & '-' & $agetype)
        FileDelete ($files_array[$file])
;~         DirRemove ($file, 1) ; Did you really want to delete a dir and all files in that dir even though it might hold newer files?
    EndIf
Next

FileWriteLine($log_file_name, "")





;Original Author ?????
;UDF has been modified multiple times
;
;_FileSearch( "Path and Mask", <$nOption>, <$cpusaver>, <$dirfilter>, <$filefilter>)
;
;----PARAMETERS-----
;$nOption -   <Optional (0 - normal, 1- recursive)>
;$cpusaver -  <Optional (0 - normal, 1 - CPU Friendly, but Slower)>
;$dirfilter - <Optional (0 - list directories, 1 - filter out directories)>
;$filefilter- <Optional (0 - list files, 1 - filter out files)>
;
;----RETURN-----
; Returns array. Either Array of files (full path) where...
; Array[0] is number of files.
; Array[0] = 0 if nothing found.
; EXAMPLE USAGE
;--------------------------------------------
;~ #include<array.au3>
;~ $a = _FileSearch("C:\*.*", 1, 0, 0, 0)
;~ _ArrayDisplay($a)
;--------------------------------------------
Func _FileSearch($szMask, $nOption = 0, $cpusaver = 0, $dirfilter = 0, $filefilter = 0)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]

    If Not StringInStr($szMask, "\") Then
        $szRoot = @ScriptDir & "\"
    Else
        While StringInStr($szMask, "\")
            $szRoot = $szRoot & StringLeft($szMask, StringInStr($szMask, "\"))
            $szMask = StringTrimLeft($szMask, StringInStr($szMask, "\"))
        WEnd
    EndIf
    If $nOption = 0 Then
        _FileSearchUtil($szRoot, $szMask, $szReturn, $cpusaver, $dirfilter, $filefilter)
    Else
        While 1
            $hFile = FileFindFirstFile($szRoot & "*.*")
            If $hFile >= 0 Then
                $szBuffer = FileFindNextFile($hFile)
                While Not @error
                    If $szBuffer <> "."  And $szBuffer <> ".."  And _
                            StringInStr(FileGetAttrib($szRoot & $szBuffer), "D") Then _
                            $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                    $szBuffer = FileFindNextFile($hFile)
                WEnd
                FileClose($hFile)
            EndIf
            _FileSearchUtil($szRoot, $szMask, $szReturn, $cpusaver, $dirfilter, $filefilter)
            If $szPathList == "*"  Then ExitLoop
            $szPathList = StringTrimLeft($szPathList, 1)
            $szRoot = StringLeft($szPathList, StringInStr($szPathList, "*") - 1) & "\"
            $szPathList = StringTrimLeft($szPathList, StringInStr($szPathList, "*") - 1)
        WEnd
    EndIf
    If $szReturn = "" Then
        $aNULL[0] = 0
        Return $aNULL
    Else
        Return StringSplit(StringTrimRight($szReturn, 1), "*")
    EndIf
EndFunc   ;==>_FileSearch

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN, $cpusaver, $dirfilter, $filefilter)
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
        $szBuffer = FileFindNextFile($hFile)
        While Not @error
            If $cpusaver = 1 Then Sleep(1)                                  ;OPTIONAL FOR CPU SAKE
            If $szBuffer <> "."  And $szBuffer <> ".."  Then
                If StringInStr(FileGetAttrib($ROOT & $szBuffer), "D") Then
                    If $dirfilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*"
                Else
                    If $filefilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*"
                EndIf
            EndIf
            $szBuffer = FileFindNextFile($hFile)
        WEnd
        FileClose($hFile)
    EndIf
EndFunc   ;==>_FileSearchUtil

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...