This should do the trick. Commented the last bit just in case you're not familiar with ruby.
#!/usr/bin/ruby
require 'find'
directory = "/home/peter/Ruby/test_data"
files = Array.new
#first get a list of the files
Find.find(directory) do |filename|
filename.each do |f|
files << f if /\.dat/ =~ f
end
end
#to hold the difference, closest file name and its difference in time from today
diff, closest, closest_diff = nil
files.each do |f|
#get the part of the string following the last /
filename = f.split("/")[-1]
#create a time using the info in the filename
x = Time.utc(filename.slice(17,4), filename.slice(15,2), filename.slice(13,2))
#if the time is greater than now
if x > Time.now
#calculate the difference
diff = x - Time.now
if closest.nil?
#if closest is empty (ie we are checking the first file)
closest = f
closest_diff = diff
else
#otherwise if the difference is smaller than the closest diff update our variables
closest = f if diff < closest_diff
closest_diff = diff
end
end
end
puts closest