Jump to content

Delete Files older than x Minutes


up2late
 Share

Recommended Posts

Greetings,

I've looked through the forums but haven't had much success.  I am trying to write a script that takes a command line parameter of a path and deletes files older than 5 minutes.  I've hacked a few together but still can't get it working.  Can someone help me figure out where I went wrong?

I've attached the script I am working on and have my basic error checking included.

The array is working to determine which files need to be deleted, but the delete isn't occurring.  I am thinking it may be with my arrary function in the delete command?

Thanks for all of the help already.  Still new to the forums.

#include <File.au3>
#include <Array.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

Dim $FilePath
If $CmdLine[0] <> 1 Then
Exit
Else
    $FilePath = $CmdLine[1]
    ; List all the files and folders in the desktop directory using the default parameters and return the full path.
    Local $aFileList = _FileListToArray($FilePath, Default, 1, Default)
    $date = _NowCalc()
    If IsArray($aFileList) Then
        For $i = 1 To $aFileList[0]
            $CreateTime = FileGetTime($FilePath & $aFileList[$i], 1)
            $CreateTimeArray = $CreateTime[0] & "/" & $CreateTime[1] & "/" & $CreateTime[2] & " " & $CreateTime[3] & ":" & $CreateTime[4] & ":" & $CreateTime[5]
            If _DateDiff('n', $CreateTimeArray, $date) > 3 Then
                ;MsgBox ("Test", $i,"")
                FileDelete($FilePath & $aFileList[$i])
            EndIf
        Next
        _ArrayDisplay($aFileList, "$aFileList")
    EndIf
EndIf
Edited by up2late
Link to comment
Share on other sites

Where is it failing?

Have you checked to see what FileGetTime is returning and what _NowCalc is returning and what $CreateTimeArray contains?

Are you sure that the creation time is what you're looking to get from FileGetTime and not the last modified time? Depending upon where the files are coming from there might be a difference.

is there a trailing backslash at the end of the $FilePath variable? Otherwise the path is going to look like "C:SomePathSomefilename.exe" rather than "C:SomePathSomefilename.exe"

Are any files returned from _FileListToArray?

You need error checking in your script before you can understand where it's not working. As posted you have none.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

In my signature there is a function for checking the file date difference.

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

A proper date checking script, no doubt like the one by guinness, will also check the date, not just time, so that a few minutes after midnight (for example), will not be seen as older than a few minutes before midnight.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

I was just saying, and hadn't really looked at his code ... never do, when AutoIt code tags haven't been used ... unless it is only two or three lines. I just tend to read the responses of others I respect and see whether they have it in hand. My comment was really for those who may require it.

I see things here, to be as much about teaching, as it is about answering a question or request.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Thanks everyone for the help.  I have modified the original post with more information.  Still new to the forums and AutoIt.  I have the script working to determine which files need to be deleted, but the actual delete isn't occurring.  I am wondering if it's something with the FileDelete command and passing array values in?

Any help would be greatly apprciated.

Thanks again.

Link to comment
Share on other sites

I think this has already been mentioned above, but you need to indicate the full filename including path for the FileDelete to work as desired. You can either change the call to _FileListToArray so that it returns the path (see the $bReturnPath parameter) or you can change your call to FileDelete so that it specifies the directory along with the filename.

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