Sign in to follow this  
Followers 0
Olli

Delete oldest files in directory

2 posts in this topic

Hi,

Is there anyone how knows how to remove the 10 oldest files in a directory when the file count is bigger then 100?

Thx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

like this:

#include <Array.au3>
#include <File.au3>
 
Local $folder, $aFileName, $aTmp
Local $filecount, $filetime, $filename, $filepath
 
$folder = "add folder path here!!!"
 
$aFileName = _FileListToArray($folder, "*", 1)
 
If @error = 1 Then
  MsgBox(0, "", "No Folders Found.")
  Exit 1
EndIf
 
If @error = 4 Then
  MsgBox(0, "", "No Files Found.")
  Exit 1
EndIf
 
;~ _ArrayDisplay($aFileName, "$aFileName")
 
$filecount = $aFileName[0]
If $filecount < 100 Then
  MsgBox(0, "result", "file count in folder is less than 100")
  Exit 1
EndIf
 
For $i = 1 To $filecount
  $filename = $folder & "\" & $aFileName[$i]
  ;0 - Modified (default)
  ;1 - Created
  ;2 - Accessed
  ;use 0 to get file modified time!!!
  $filetime = FileGetTime($filename, 0, 1)
  $filename = $filetime & "|" & $filename
  $aFileName[$i] = $filename
Next
 
_ArraySort($aFileName)
 
For $i = 1 To 10
  $filename = $aFileName[$i]
  $aTmp = StringSplit($filename, "|")
  ;~  _ArrayDisplay($aTmp, "$aTmp")
  $filepath = $aTmp[2]
  ConsoleWrite('$filepath = ' & $filepath & @CRLF) ;### Debug Console
  ;!!!DELETE FILE!!!
  ;~  FileDelete($filepath)
Next
Exit 0
Edited by yahaosoft

Thanksgiving...

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