 |
|
| Computers Forum Index » Computer Languages (Ruby) » mtime.max... |
|
Page 1 of 1 |
|
| Author |
Message |
| Derek Smith... |
Posted: Sat Oct 31, 2009 5:17 am |
|
|
|
Guest
|
Hi All,
My goal is to get the oldest file using mtime.to_i then delete it.
However my
puts stats.to_a.max does not seem to print out the oldest mtime
data/file, rather its printing out the 2nd to oldest file;
prod_db_bkup_Tue2009-10-271455.gz
Correct me please and show me the code.
thx!
<snip>
d=Date.today
t=Time.now
require 'enumerator'
stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime.to_i]
end
puts stats.to_a,"\n\n"
puts stats.to_a.max
end
prod_DB_OS_bkup.rb: 90 lines, 2627 characters.
[root at (no spam) vixxxxx /usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
1256828444
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
1256828056
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz
1256913745
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
1256827961
/usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz
1256669730
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Derek Smith... |
Posted: Sat Oct 31, 2009 5:17 am |
|
|
|
Guest
|
|
| Back to top |
|
|
|
| Derek Smith... |
Posted: Sat Oct 31, 2009 5:17 am |
|
|
|
Guest
|
Derek Smith wrote:
Quote: Hi All,
My goal is to get the oldest file using mtime.to_i then delete it.
However my
puts stats.to_a.max does not seem to print out the oldest mtime
data/file, rather its printing out the 2nd to oldest file;
prod_db_bkup_Tue2009-10-271455.gz
Correct me please and show me the code.
thx!
snip
d=Date.today
t=Time.now
require 'enumerator'
stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime.to_i]
end
puts stats.to_a,"\n\n"
puts stats.to_a.max
end
prod_DB_OS_bkup.rb: 90 lines, 2627 characters.
[root at (no spam) vixxxxx /usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
1256828444
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
1256828056
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz
1256913745
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
1256827961
/usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz
1256669730
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168
ls -alrt /usr/local/vrep/prod*
-rw-r--r-- 1 root wheel 38608031 Oct 27 14:55
/usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz
-rw-r--r-- 1 root wheel 38608186 Oct 27 16:42
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
-rw-r--r-- 1 root wheel 38608780 Oct 29 10:52
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
-rw-r--r-- 1 root wheel 38608793 Oct 29 10:54
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
-rw-r--r-- 1 root wheel 38608802 Oct 29 11:00
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
-rw-r--r-- 1 root wheel 38609193 Oct 30 10:42
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Derek Smith... |
Posted: Sun Nov 01, 2009 5:52 am |
|
|
|
Guest
|
Quote: puts stats.sort {|x,y| x[1] <=> y[1]}.max
This explicitly sorts by the 2nd value in the to_a array, which is the
mtime.
Thanks, but that does not work because its sorting on the time only, for
example sorting on 1052, 1054, 110, 1042.
root wheel 38608780 Oct 29 10:52
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
root wheel 38608793 Oct 29 10:54
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
root wheel 38608802 Oct 29 11:00
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
root wheel 38609193 Oct 30 10:42
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz
so I am using this, but I could not get my each loop to work
please help!
stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime.to_i]
end
statsA = stats.sort { |x,y| x[1] <=> y[1] }.to_a
oldest = statsA[0][1].to_s.to_i
p statsA
p "1st oldest:, oldest
(0..statsA.length-1).step(1) do |i|
if #{statsA[i][1]} < #{oldest}
oldest = statsA[i][1]
end
end
p "after if clause oldest:", oldest
__OUTPUT__
[["/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz", [1256827961]],
["/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz", [1256828056]],
["/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz", [1256828444]],
["/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz", [1256913745]]]
"1st oldest:"
[1256827961]
"after if clause oldest:"
[1256913745]
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Derek Smith... |
Posted: Mon Nov 02, 2009 6:17 am |
|
|
|
Guest
|
Quote: Why do you convert the mtime (a Time object) to an integer? Time
objects are comparable. Try to omit the ".to_i" and compare the
Time objects that represent the mtimes.
Regards,
Daniel
HI!
I feel like Im smoking crack and getting pissed off at the same time! No
offence intended!
This is still not working as I should be seeing
/backups/prod_db_bkup_Thr2009-10-291052.gz
as the oldest file.
require 'time'
stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime]
end
stats_sorted = Hash.new
stats_sorted = stats.sort { |x,y| x[1]<=>y[1] }
oldest = stats_sorted[0][1].first
p oldest
oldestfile = ''
stats.each_value do |v|
if #{v} < #{oldest}
oldest = v
oldestfile = stats.index(oldest)
end
end
puts oldestfile
puts "exit"
exit
end
prod_DB_OS_bkup.rb: 108 lines, 2997 characters.
[root at (no spam) .../usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb
Thu Oct 29 10:52:41 -0400 2009
/backups/prod_db_bkup_Thr2009-10-291054.gz
exit
[root at (no spam) .../usr/local/vrep/OS_scripts]# ls -alrt /backups/prod*.gz
-rw-r--r-- 1 root wheel 38608780 Oct 29 10:52
/backups/prod_db_bkup_Thr2009-10-291052.gz
-rw-r--r-- 1 root wheel 38608793 Oct 29 10:54
/backups/prod_db_bkup_Thr2009-10-291054.gz
-rw-r--r-- 1 root wheel 38608802 Oct 29 11:00
/backups/prod_db_bkup_Thr2009-10-29110.gz
-rw-r--r-- 1 root wheel 38609193 Oct 30 10:42
/backups/prod_db_bkup_Fri2009-10-301042.gz
-rw-r--r-- 1 root wheel 38770872 Nov 1 16:29
/backups/prod_db_bkup_Sun2009-11-011629.gz
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Mon Nov 30, 2009 9:34 pm
|
|