Jump to content

Delete files older than 5 minutes


Recommended Posts

Hi:

First I apologize for creating another post on this topic but I have reviewed the forums and tried multiple snippets and ideas from others but cannot get this to work. I have a directory called 'Test' and what I need to do is check it to see if there is a file that is older than 5 minutes from the current time and if so, delete it. I can get the array to work and I can get the time of the files contained therein but I cannot loop through it. I appreciate any and all help! Thank you in advance

[#include <Date.au3>

#Include <File.au3>

#include <array.au3>

$files = _FileListToArray("C:\Test", "*.pdf", 2) ;create an array of files in the specified folder

$date = _NowCalc()

$newdate=_DateAdd("D",-5,$date) ;subtract 5 minutes

$formatdate=StringSplit($newdate,"/") ;removing the /

$newdate=$formatdate[1]&$formatdate[2]&$formatdate[3]&@HOUR&@MIN&@SEC

If IsArray($files) Then

For $i = 1 To UBound($files) - 1

$CreateTime = FileGetTime("C:\Test" & $files[$i], 1, 1)

If $CreateTime > $newdate Then

FileDelete($files[$i])

EndIf

Next

EndIf]

Link to comment
Share on other sites

Have a look at this >>

Edit: This is for days only!

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Hi,

use

$newdate = _DateAdd("n",-5,$date)

for getting 5 minutes of. In your post you used "D" => subtracts days.

Then go ahead and remove all "/" , " " and ":" from the date to get this format:

YYYYMMDDHHMMSS

With this format you can evaluate if the file is newer.

:)

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Here's another way to do it:

#include <Date.au3>
#Include <File.au3>

$files = _FileListToArray("C:\Test", "*.pdf", 1) ;create an array of files in the specified folder
$date = _NowCalc()
If IsArray($files) Then
    For $i = 2 To $files[0]
        $CreateTime = FileGetTime("C:\Test\" & $files[$i], 1)
        $CreateTimeArray = $CreateTime[0] & "/" & $CreateTime[1] & "/" & $CreateTime[2] & " " & $CreateTime[3] & ":" & $CreateTime[4] & ":" & $CreateTime[5]
        If _DateDiff('n', $CreateTimeArray, $date) > 5 Then ConsoleWrite($files[$i] & " should be deleted!" & @LF) ;FileDelete($files[$i])
    Next
EndIf

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

I don't know why everybody insists on #Including files when it is so simple to do it with native AutoIt calls. I never use a UDF unless I require at least 30% of the code in there.

$sFile = @ScriptFullPath
$iFileDate = Number(StringTrimRight(FileGetTime($sFile, 1, 1), 2))
$iNowDate = Number(@YEAR & @MON & @MDAY & @HOUR & @MIN)
$iDiff = $iNowDate - $iFileDate
;MsgBox(0, "File Time", $iFileDate & @CRLF & $iNowDate & @CRLF & @CRLF & $iDiff)
If $iDiff >= 5 Then
    MsgBox(0, "Result", "The file " & $sFile & " is older than 5 minutes.")
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Well Geo, not all of us have a wealth of experience from which to draw insightful examples like you ;)

Most of us are, how should I put it, "Young in the business" :)

Very intuitive minimalistic example as always ;) I like it.

Link to comment
Share on other sites

It really could be made simpler than that. I just prefer to keep things as simple as possible and that includes not using #Include until I have a situation where it makes no sense NOT to use it. I also often just copy the required functions from a UDF as opposed to including the whole UDF.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

I don't know why everybody insists on #Including files when it is so simple to do it with native AutoIt calls. I never use a UDF unless I require at least 30% of the code in there.

$sFile = @ScriptFullPath
$iFileDate = Number(StringTrimRight(FileGetTime($sFile, 1, 1), 2))
$iNowDate = Number(@YEAR & @MON & @MDAY & @HOUR & @MIN)
$iDiff = $iNowDate - $iFileDate
;MsgBox(0, "File Time", $iFileDate & @CRLF & $iNowDate & @CRLF & @CRLF & $iDiff)
If $iDiff >= 5 Then
    MsgBox(0, "Result", "The file " & $sFile & " is older than 5 minutes.")
EndIf

George,

This is not a valid test when the minutes are around the full hour mark!

You have to convert the time to a decimal value for else it will not work all the time.

Jos :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I also often just copy the required functions from a UDF as opposed to including the whole UDF.

Me too :) well not me really,
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/striponlyincludes
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Link to comment
Share on other sites

George,

This is not a valid test when the minutes are around the full hour mark!

You have to convert the time to a decimal value for else it will not work all the time.

Jos :)

I don't see it but I'll take your word for it. Since it's returning the number of Date/Month/Day/Minute then only Seconds could throw it off a bit and I'm not using them.

NM, I see it now. That can be solved with a bit of math.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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