if file exists and is older than x hours

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

if file exists and is older than x hours

Post by theonex » Sat Aug 19, 2017 11:02 am

Hi to all


I know how to use the iffileexists statment but i was wondering if there was a way to check if a file exists and is older than x hours.

e.g
iffileexists>test.text
check if file is older than x hours
if yes do something
else
do something else
endif

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: if file exists and is older than x hours

Post by JRL » Mon Aug 21, 2017 2:21 pm

This example tells you how old a file is in seconds.

Code: Select all

Input>vFile,Pick a file
IfFileExists>vFile
  FileDate>vFile,vFileDate
  MidStr>vFileDate,1,4,yyyy
  MidStr>vFileDate,5,2,mm
  MidStr>vFileDate,7,2,dd
  FileTime>vFile,vFileTime
  MidStr>vFileTime,1,2,hh
  MidStr>vFileTime,3,2,mn
  MidStr>vFileTime,5,2,ss
  VBEVal>DateDiff("s","%mm%/%dd%/%yyyy% %hh%:%mn%:%ss%",now()),vSecondsOld
EndIf

MDL>vSecondsOld

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: if file exists and is older than x hours

Post by theonex » Tue Sep 19, 2017 8:56 am

hello JRL

i really appreciate your help and assitance with this. i have a patch file that is run at the very end of my exe script. if the patch file is triggered it means the script ran successfully in which the patch file creates a notepad file e.g abc_todays date. i want to check if abc_todays date exist and if its older than 24 hours before the exe script is re-run again.

how would i go about using the sript you provided me with above to do this?

your reply is appreciated.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: if file exists and is older than x hours

Post by JRL » Tue Sep 19, 2017 1:48 pm

I'd place this first block of code at the beginning of your exe script and run the exe several times per day. Or alternatively and the way I would likely do this. I'd put the second block of code at the start of the exe script and have the exe run continuously.

First block of code:

Code: Select all

//Not sure how you know the name of the file but its unlikely
//it will be hard coded like this example.  If you need help with that
//we'll need to know more about your naming convention.

Let>vFile=abc_todays date

IfFileExists>vFile
  FileDate>vFile,vFileDate
  MidStr>vFileDate,1,4,yyyy
  MidStr>vFileDate,5,2,mm
  MidStr>vFileDate,7,2,dd
  FileTime>vFile,vFileTime
  MidStr>vFileTime,1,2,hh
  MidStr>vFileTime,3,2,mn
  MidStr>vFileTime,5,2,ss
  VBEVal>DateDiff("s","%mm%/%dd%/%yyyy% %hh%:%mn%:%ss%",now()),vSecondsOld
  If>%vSecondsOld%>86400
    //The file is more than 24 hours old so the script can continue.
  Else
    //The file is less than 24 hours old so exit the script.
    Exit>0
  EndIf
EndIf

//The rest of the script
Second block of code

Code: Select all

//Key down onEvent> to kill executable without having to go to Task Manager
//Key combination is WinKey + Esc
OnEvent>key_down,VK27,8,Quit
SRT>Quit
  Exit>0
END>

Label>TheBeginningOfTheScript
Let>vFile=abc_todays date

IfFileExists>vFile
  FileDate>vFile,vFileDate
  MidStr>vFileDate,1,4,yyyy
  MidStr>vFileDate,5,2,mm
  MidStr>vFileDate,7,2,dd
  FileTime>vFile,vFileTime
  MidStr>vFileTime,1,2,hh
  MidStr>vFileTime,3,2,mn
  MidStr>vFileTime,5,2,ss
  VBEVal>DateDiff("s","%mm%/%dd%/%yyyy% %hh%:%mn%:%ss%",now()),vSecondsOld
  If>%vSecondsOld%>86400
    //The file is more than 24 hours old so the script can continue.
  Else
    //The file is less than 24 hours old so wait 60 seconds then goto TheBeginningOfTheScript.
    Wait>60
    Goto>TheBeginningOfTheScript
  EndIf
EndIf

//The rest of the script
//At the end of your script you need to return to the beginning so the script runs forever.
Goto>TheBeginningOfTheScript

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: if file exists and is older than x hours

Post by theonex » Tue Sep 19, 2017 2:30 pm

I really appreciate your time JRL.

The naming convention used is as follows and since the naming always changes i would need the code to detect this.
abc_17092017
abc_18092017
abc_19092017

the above naming would be what the patch file generates at the end of the exe script the time that the txt files are created varies. i hope this answers the naming convention.

a new txt file is created every day that the exe runs successfully so the there has to be a way to only check the age of the last file created.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: if file exists and is older than x hours

Post by JRL » Tue Sep 19, 2017 3:35 pm

Can use the GetNewestFile> function. The following should get the latest file name and set it to variable "vFile"

Code: Select all

//Set the path to the files
//Leave off the final backslash
Let>vFilePath=YourDrive:\YourPath
GetNewestFile>%vFilePath%\abc_*,vFile

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: if file exists and is older than x hours

Post by theonex » Tue Sep 19, 2017 5:05 pm

almost there i really appreciate your patience.

i am still in testing phase. when i do this the code works fine

Code: Select all

Let>vFile=abc_todays date
IfFileExists>vFile
  FileDate>vFile,vFileDate
  MidStr>vFileDate,1,4,yyyy
  MidStr>vFileDate,5,2,mm
  MidStr>vFileDate,7,2,dd
  FileTime>vFile,vFileTime
  MidStr>vFileTime,1,2,hh
  MidStr>vFileTime,3,2,mn
  MidStr>vFileTime,5,2,ss
  VBEVal>DateDiff("s","%mm%/%dd%/%yyyy% %hh%:%mn%:%ss%",now()),vSecondsOld
  If>%vSecondsOld%>86400
    //The file is more than 24 hours old so the script can continue.
  Else
    //The file is less than 24 hours old so exit the script.
    Message>file not 24 hours old
    wait>5

    Exit>0
  EndIf
EndIf



Message>file 24 hours old
wait>5

how do i make the

Code: Select all

Let>vFile=abc_todays date
use the file variable from

Code: Select all

//Set the path to the files
//Leave off the final backslash
Let>vFilePath=YourDrive:\YourPath
GetNewestFile>%vFilePath%\abc_*,vFile
i tried i tried

Code: Select all

Let>vFile=vFile
many thanks for your help

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: if file exists and is older than x hours

Post by JRL » Tue Sep 19, 2017 5:12 pm

Code: Select all

GetNewestFile>%vFilePath%\abc_*,vFile
Sets the value of variable "vFile" to the file name.

Remove the line

Code: Select all

Let>vFile=
it is resetting the variable to an unwanted value.

You want something like this. Make sure your path is correct.

Code: Select all

//Set the path to the files
//Leave off the final backslash
Let>vFilePath=YourDrive:\YourPath
GetNewestFile>%vFilePath%\abc_*,vFile

IfFileExists>vFile
  FileDate>vFile,vFileDate
  MidStr>vFileDate,1,4,yyyy
  MidStr>vFileDate,5,2,mm
  MidStr>vFileDate,7,2,dd
  FileTime>vFile,vFileTime
  MidStr>vFileTime,1,2,hh
  MidStr>vFileTime,3,2,mn
  MidStr>vFileTime,5,2,ss
  VBEVal>DateDiff("s","%mm%/%dd%/%yyyy% %hh%:%mn%:%ss%",now()),vSecondsOld
  If>%vSecondsOld%>86400
    //The file is more than 24 hours old so the script can continue.
  Else
    //The file is less than 24 hours old so exit the script.
    Message>file not 24 hours old
    wait>5
    Exit>0
  EndIf
EndIf

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: if file exists and is older than x hours

Post by theonex » Tue Sep 19, 2017 5:37 pm

works like a charm.


once again thank you so much for all the time you spent helping me get this right.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts