#211171 - 2016-03-07 06:07 PM
How to get the date for Monday
|
shayaan
Just in Town
Registered: 2016-03-07
Posts: 1
Loc: NJ
|
My predecessor created a script that generates files like this
MMDDYYYY-@USERIDAM.txt
MMDDYYYY-@USERIDPM.txt
@USERID is the Windows AD name
The date of the file is the Monday of each week of the year
Example
03072016-johnsmithAM.txt 03072016-johnsmithPM.txt
I need to create a script that will delete the file for the current week (not the weeks in the future)
I am not sure how to do this in Kix
|
|
Top
|
|
|
|
#211175 - 2016-03-08 03:51 AM
Re: How to get the date for Monday
[Re: Allen]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
If I understand this.. you want to find the date of "Monday" from the current week, so that come, say Thursday, you can delete Monday's file?
It's all about time arithmetic...
The @DAY function returns the number of days since Sunday, so Monday is 1, Tuesday is 2, etc..
Each day has 86,400 seconds, and seconds is a standard way of expressing time values, called a "cTime". Technically, it's the number of seconds since an "Epoch" (an artificial "beginning of time", like Jan 1, 1900).
The TimeConvert UDF converts a date/time string to a cTime value and cTime values back to date/time strings. The TimeConvert UDF uses a standard epoch value, but you can define your own to use smaller values, like "@YEAR + '/01/01'".
So - what was the date this past Monday? How can I use that to create the file name to delete? Here's a working example using the TimeConvert UDF.$Today = TimeConvert(@DATE + ' 00:00:00') ; Get the cTime for today at midnight
$DDay = (Val(@WDAYNO) - 1) * 86400 ; Calculate the number of seconds since Monday
$Monday = TimeConvert($Today - $DDay) ; Calculate the cTime for Monday by subtracting the elapsed seconds
' Today: ' @DATE ' / ' $Today ? ; show the Today cTime (debugging)
' DDay: ' $DDay ? ; show the elapsed secs (debugging)
'Monday: ' Split($Monday, ' ')[0] ? ; show the date for Monday (debugging)
; reformat the date into the computer-unfriendly format ;)
$FileDate = SubStr($Monday, 6, 2) + SubStr($Monday, 9, 2) + Left($Monday, 4)
; Delete the file(s) from Monday
'Deleting files from ' $FileDate ?
Del $FileDate + '*.txt' ; wildcard file delete for matching dates The same idea can be used for any day, and for longer periods (1 week = 86400*7 seconds)
Glenn
PS - if you want to use TimeConvert, you can get the latest version from the Kixtart pages of my web site - over 200 UDFs are available.
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 937 anonymous users online.
|
|
|