Jump to content

Sleep() count per day to run FileDelete()


Recommended Posts

Hi Experts,

I just need your help with the counting for Sleep() to count 10 days and then do the FileDelete(). Also, I need this little code to running 24/7 in a server.

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

Sleep(60000) ;60 for 1 minute is this correct?
$sFldr2 = "D:\Programs\Test\Temp\"
FileDelete($sFldr2 & "*.*")
Local $File = FileOpen("D:\Programs\Test\Temp.log", 1)
FileWriteLine($File, '"Deleted files in Temp folder" ' & "on " & _DateTimeFormat(_NowCalc(), 2) &" at "& _NowTime())
FileClose($File)

I got confused with it and little bit hanging on how to run it 24/7.:>

Also, if you have any better way to do it, then, that would be great.:sweating:

 

Thanks, Expert in advance.

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

  • Moderators

       11  KickStarter15,

I would use _DateDiff rather than Sleep - something like this (untested):

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

; Use a HotKey to exit if necessary
HotKeySet("{ESC}", "_Exit")

$sFldr2 = "D:\Programs\Test\Temp\"

; Set values for today's date
$sDay = @MDAY
$sFormattedDate = _NowCalcDate()

; Start an infinte loop
While 1

    ; Check when day changes
    If $sDay <> @MDAY Then
        
        ; Reset stored day so we only check once a day
        $sDay = @MDAY

        ; Check if 10 days passed since last delete
        If _DateDiff("D", $sFormattedDate, _NowCalcDate()) > 10 Then
            ; If so then run the code
            FileDelete($sFldr2 & "*.*")
            Local $File = FileOpen("D:\Programs\Test\Temp.log", $FO_APPEND)
            FileWriteLine($File, '"Deleted files in Temp folder" ' & "on " & _DateTimeFormat(_NowCalc(), 2) &" at "& _NowTime())
            FileClose($File)
            
            ; Reset the date so we wait for another 10 days
            $sFormattedDate = _NowCalcDate()
            
        EndIf
        
    EndIf
    
WEnd

Func _Exit()
    Exit
EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23,

Thanks, does this mean if I want to test it, like lets have it 1 day to see if its working. Should i modify the below:

From

If _DateDiff("D", $sFormattedDate, _NowCalcDate()) > 10 Then

To

If _DateDiff("D", $sFormattedDate, _NowCalcDate()) > 1 Then

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@AspirinJunkie,

Thanks, but the code is for my other script and I will add this into that script.

@Melba23,

Is there a way that I can test your code using only minutes? I need to test this code first but i think its a per day count.

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@Melba23, I got it. I tested it by doing below and it's working well. :lol:, thank you so much.

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

$sFormattedDate = "2017/08/25" ; I need to manaully input the date to get the greater sum of 10 in _DateDiff()

If _DateDiff("D", $sFormattedDate, _NowCalcDate()) > 10 Then
   MsgBox(0,"","10 days already")
Else
   MsgBox(0,"","Wait after 10 days")
EndIf
Exit

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@Melba23,

Sorry for this but I need to ask something. When I ran my program with an infinite loop, my CPU usage increases rapidly (ranging from 60% to 88%) and what I did is I add sleep(10) function within the "While.." where the infinite loop starts.

My question is, does this will affect the loop? however, the CPU usage was controlled with sleep(). Is this okay?^_^

While 1

    ; Check when day changes
    If $sDay <> @MDAY Then
        
        ; Reset stored day so we only check once a day
        $sDay = @MDAY

        ; Check if 10 days passed since last delete
        Sleep(10) ; to slowdown the usage of my CPU
        If _DateDiff("D", $sFormattedDate, _NowCalcDate()) > 10 Then
            ; If so then run the code
            FileDelete($sFldr2 & "*.*")
            Local $File = FileOpen("D:\Programs\Test\Temp.log", $FO_APPEND)
            FileWriteLine($File, '"Deleted files in Temp folder" ' & "on " & _DateTimeFormat(_NowCalc(), 2) &" at "& _NowTime())
            FileClose($File)
            
            ; Reset the date so we wait for another 10 days
            $sFormattedDate = _NowCalcDate()
            
        EndIf
        
    EndIf
    
WEnd

 

Thanks!

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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