Community discussions

MikroTik App
 
crs
just joined
Topic Author
Posts: 18
Joined: Wed Mar 10, 2010 10:19 am

find files older than date

Tue Apr 10, 2012 2:29 pm

Hi.

Is there any way to find files matching pattern and older than give date? I can find files by pattern (name starting with backup) using:
/file find name~'^backup*' 
but i don't know how to limit list only to older files.

Second thing - as I found list of files found by "find" is not sorted. Any way to have it in order?
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: find files older than date

Wed Apr 18, 2012 12:10 am

Yes, there are ways to find files matching a pattern older than a particular date. How you go about it probably depends on what you're trying to do with the information.

The following script is meant to be copied / pasted into a terminal window. It does a few things:
1. It loops through all the files and compares each file's date to today's date (assuming your routerboard is set to the correct date!), and it calculates how many days ago this was.
2. It checks each file name for the pattern you specify.
3. It outputs a list of matching files in the terminal window.

So, you'll want to edit the first couple variables for how many "days ago" you want, and the "pattern" you want matched.
# Only show files older than X days ago
# tested on RouterOS v5.7
{
	# how many days ago
	:local daysAgo 7;
	
	# only show files that contain this in their name
	:local filter "backup";

	# months array
	:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
	# get current date
	:local curDate [ /system clock get date ];
	# extract current month
	:local curMonth [ :pick $curDate 0 3 ];
	# get position of our month in the array = month number
	:set curMonth ([ :find $months $curMonth -1 ] + 1);
	# extract current day
	:local curDay [ :pick $curDate 4 6 ];
	# extract current year
	:local curYear [ :pick $curDate 7 11 ];

	# loop through all files
	:foreach i in=[/file find] do={
		# get this file's creation time
		:local fileDate [/file get number="$i" creation-time]
		# extract the date
		:set fileDate [ :pick $fileDate 0 11 ];
		# extract the month
		:local fileMonth [ :pick $fileDate 0 3 ];
		# get position of our month in the array = month number
		:set fileMonth ([ :find $months $fileMonth -1 ] + 1);
		# extract the day
		:local fileDay [ :pick $fileDate 4 6 ];
		# extract the year
		:local fileYear [ :pick $fileDate 7 11 ];
		
		# the sum of total days
		:local sum 0;
		# subtract the file's year from the current year, multiply times 365 to get approx days, add to sum
		:set sum ($sum + (($curYear - $fileYear)*365));
		# subtract the file's month from the current month, multiply times 30 to get approx days, add to sum
		:set sum ($sum + (($curMonth - $fileMonth) * 30));
		# subtract the file's day from the current day, add to sum
		:set sum ($sum + ($curDay - $fileDay));
		# if the sum is greater than or equal to our daysAgo and the file name contains our filter
		:if ($sum >= $daysAgo && [/file get number="$i" name]~"$filter") do={
			# show file info to the terminal
			:put ([/file get number="$i" creation-time ] . "\t" . [/file get number="$i" name] . " - " . "$sum days ago");
		}
	}
}
NOTICE: The "days ago" calculation is not exact! It is a rough calculation because leap year and the exact number of days in each month are not taken into account. If anyone is able to provide a concise way to calculate this, that would be helpful.

As for your sorting question, I don't know about that.