Jump to content

Recommended Posts

Posted

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.

Posted

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!

  Reveal hidden contents

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

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
Posted

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!

  Reveal hidden contents

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

Posted

Look at the help file about If statements.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

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!

  Reveal hidden contents

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

Posted

  On 12/18/2012 at 5:00 PM, 'BrewManNH said:

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

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

Posted

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

  • 2 weeks later...

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...