Help please, How to auto delete oldest file

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

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.

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].
:foreach i in=[find name~"Video"] do={

Thanks Skot,
It’s great. Work fine