Support Portal

for ProcessRobot and WinAutomation

Start a new topic
Answered

How to Remove Files older than a date

Hi. I'm new to WinAutomation.


I am trying to set up a simple backup routine, but I cannot figure out the best way to remove files older than a week.


Any hints would be much appreciated.


Heidi


Best Answer

Hello Heidi,


You can achieve your goal by the following sequence:

1) Get Files in Folder

- make sure you configure the list to be sorted in the Advanced tab for faster processing

2) Get Current Date and Time

3) Loop the Files List using the 'Loop' action starting from the Files.Count to 0 (you need to start from the last file of the List as it will help keep track of the indexes)

- Subtract the .CreationTime of the File from the Current Date in Days using 'Subtract Dates' 

- If the difference is <7 remove the File from the Files List using 'Remove Item from List' action. Since the List is sorted by ascending and you are looping from the end, you will need to always remove the last file and the List will not rearrange any indexes. Once the Difference is =7 you can exit the Loop as it makes no sense spending more time looping due to the List being already sorted.

4) When the loop is finished, the initial Files List will contain only the files that are older than a week.

5) Use 'Delete File(s)' or 'Move File(s)' and you can call the list directly to process all the above files at once.


I hope the above prove to be helpful.

1 Comment

ADMIN
Answer

Hello Heidi,


You can achieve your goal by the following sequence:

1) Get Files in Folder

- make sure you configure the list to be sorted in the Advanced tab for faster processing

2) Get Current Date and Time

3) Loop the Files List using the 'Loop' action starting from the Files.Count to 0 (you need to start from the last file of the List as it will help keep track of the indexes)

- Subtract the .CreationTime of the File from the Current Date in Days using 'Subtract Dates' 

- If the difference is <7 remove the File from the Files List using 'Remove Item from List' action. Since the List is sorted by ascending and you are looping from the end, you will need to always remove the last file and the List will not rearrange any indexes. Once the Difference is =7 you can exit the Loop as it makes no sense spending more time looping due to the List being already sorted.

4) When the loop is finished, the initial Files List will contain only the files that are older than a week.

5) Use 'Delete File(s)' or 'Move File(s)' and you can call the list directly to process all the above files at once.


I hope the above prove to be helpful.

Login or Signup to post a comment