Community discussions

MikroTik App
 
amee
just joined
Topic Author
Posts: 16
Joined: Mon Apr 17, 2006 3:44 pm

Help please, How to auto delete oldest file

Fri Jan 03, 2014 6:07 am

Hi all,
I have Rb751u with 32GB usb flash drive 32GB. I used that flash drive to record my ip camera. The problem is when the flash drive is full, my camera stop recording. How to script whenever the disk free space is <1gb, remove oldest file. So the router periodically run the script.
THX
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: Help please, How to auto delete oldest file

Fri Jan 03, 2014 8:09 pm

I don't have a usb flash drive to test at the moment, but I assume the disk space free shows up in /system resources. If not, the script will need to be changed. Add this script to a new schedule to run however often you want.

ros code

# if free disk space is less than 1GB
:if ([/system resource get free-hdd-space] < 1073741824) do={

  :local months {"jan";"feb";"mar";"apr";"may";"jun";"jul";"aug";"sep";"oct";"nov";"dec"}
  /file

  :local oldestFile
  :local fileName

# loop through files  
  :foreach i in=[find name~"Video"] do={
 
    :local new [get $i creation-time]
    :local nyr [:pick $new 7 11]
    :local nmo [:find $months [:pick $new 0 3]]
    :if ($nmo < 10) do={ :set nmo ("0" . $nmo); }
    :local nday [:pick $new 4 6]
    :local nhr [:pick $new 12 14]
    :local nmin [:pick $new 15 17]
    :local nsec [:pick $new 18 20]
    :local newFile ($nyr.$nmo.$nday.$nhr.$nmin.$nsec)
 
# if new date/time is less than (older) or hasn't been set
    :if ($newFile < $oldestFile || [:len $oldestFile] = 0) do={
      :set fileName [get $i name]
      :set oldestFile $newFile
    }
  }

# if a match was found  
  :if ([:len $oldestFile] > 0) do={
    /file remove [find name="$fileName"]
  }
}
This line from above only finds files that contain "Video" in the name. Change this to match your file names, or, if you want to check ALL files, simply remove the name~"..." and leave the [find].

ros code

:foreach i in=[find name~"Video"] do={
 
amee
just joined
Topic Author
Posts: 16
Joined: Mon Apr 17, 2006 3:44 pm

Re: Help please, How to auto delete oldest file

Sat Jan 04, 2014 5:17 am

Thanks Skot,
It's great. Work fine

Who is online

Users browsing this forum: No registered users and 39 guests