Sign in to follow this  
Followers 0
SuperFletch

Compare with todays date

12 posts in this topic

Hi,

I expect this question has been asked a million times - so I apologise for this...

I've searched but can't find what I'm looking for and I'm drawing a bit of a blank on where to start with it.

I have a directory which automatically receives update files every now and then.

I want to take the date of the newest file in that directory (DONE) then compare it to today's date. (Format DD/MM/YYYY)

Using "If then Else" type logic I'll then work out whether the latest file is less than a week old and if it is then carry out some actions.

Any ideas? I haven't got any code to post as I really don't know how to proceed.

Thanks.

Share this post


Link to post
Share on other sites



How are you getting the file date? You can get today's date using _Now(), _NowDate(), _NowCalc(), or _NowCalcDate() depending upon what format you want to get it in.


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Using _RunDos to run the dir command with switches on it to order the results entries by date

_RunDos('dir /O-D "C:MyDirectory" > dir.txt')

Then I get the date using filereadline and stringleft as follows:

$String = FileReadLine(@ScriptDir & 'dir.txt',6)
$LatestDate = StringLeft($String,10)

Todays date I was getting in the same format (DD/MM/YYYY) as follows;

$TodaysDate = (@MDAY & "/" & @MON & "/" & @YEAR)
Edited by SuperFletch

Share this post


Link to post
Share on other sites

If you use FileGetTime with the format parameter set to 1 you will get the file's date and time in a string value "1 = return a string YYYYMMDDHHMMSS" and you can pick that apart to get it in the format you need. You can then compare it to today's date using

$TodaysDate = @YEAR & @MON & MDAY

This way the two dates are in the same format and can be compared easily using an If statement.


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for that - I've never used "FileGetTime" before.

It's the If statement(s) I don't know where to start with.

Edited by SuperFletch

Share this post


Link to post
Share on other sites

Look at the help file about If statements.

1 person likes this

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

Here's a short version you can adapt as needed.

$FileDate = FileGetTime("somefile.txt", 0, 1); Returns the file's modified date in YYYYMMDDHHMMSS format
$TodaysDate = @YEAR & @MON & @MDAY ; YYYYMMDD format
If StringLeft($FileDate, 8) = $TodaysDate Then ; This compares the YYYYMMDD portion of $FileDate to $TodaysDate
    ; Do something here
Endif

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

Share this post


Link to post
Share on other sites

Look at the help file about If statements.

I meant more the sort of conditions I'll be checking than how to structure them.

Share this post


Link to post
Share on other sites

Here's a short version you can adapt as needed.

$FileDate = FileGetTime("somefile.txt", 0, 1); Returns the file's modified date in YYYYMMDDHHMMSS format
$TodaysDate = @YEAR & @MON & @MDAY ; YYYYMMDD format
If StringLeft($FileDate, 8) = $TodaysDate Then ; This compares the YYYYMMDD portion of $FileDate to $TodaysDate
; Do something here
Endif

Thanks - I was going to split the dates up but this seems more sensible.

Share this post


Link to post
Share on other sites

SuperFletch,

This may be of interest. See comments in the code.

;
;  list all files in the script directory with a modify date 1 year old or older
;

#include <date.au3>

local $files = FileFindFirstFile(@scriptdir & '\*.*'), $files, $fldate, $yy, $mm, $dd

if $files = -1 then
    ConsoleWrite('No files found' & @LF)
    Exit
endif

while 1

    $file = filefindnextfile($files)
    if @error then exitloop

    ; there's a bunch of ways to parse this date, this is just how I choose to do it this time

    $fldate = filegettime($file,0,1)
    $yy = stringleft($fldate,4)
    $fldate = stringtrimleft($fldate,4)
    $mm = stringleft($fldate,2)
    $fldate = stringtrimleft($fldate,2)
    $dd = stringleft($fldate,2)

    ; _datediff calcs date/time spans...see Help file

    if _datediff('D',$yy & '/' & $mm & '/' & $dd, _nowcalc()) > 365 then
        ConsoleWrite(stringformat('%-50s %50s',$file, ' Modily Date= ' & $yy & '/' & $mm & '/' & $dd)  & @LF)
    EndIf

wend

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

This example returns the date in an array.

;
;  list all files in the script directory with a modify date 1 year old or older
;

#include <date.au3>

local $files = FileFindFirstFile(@scriptdir & '\*.*'), $files, $fldate, $yy, $mm, $dd

if $files = -1 then
    ConsoleWrite('No files found' & @LF)
    Exit
endif

while 1

    $file = filefindnextfile($files)
    if @error then exitloop

    ; return the date in an array:
    ;    [0] = year
    ;    [1] = month
    ;    [2] = day

    $fldate = filegettime($file,0)

    ; _datediff calcs date/time spans...see Help file

    if _datediff('D',$fldate[0] & '/' & $fldate[1] & '/' & $fldate[2], _nowcalc()) > 365 then
        ConsoleWrite(stringformat('%-50s %50s',$file, ' Modily Date= ' & $fldate[0] & '/' & $fldate[1] & '/' & $fldate[2])  & @LF)
    EndIf

wend

(...wasting time so I don't have to cook...)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thanks kylomas - this is the sort of thing I was looking for.

Sorry for the slow reply...

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