I am new to ruby and I'm having trouble with a script I'm working on.
It probably is something simple that I am missing. I am working on a
script to delete some files from several directories.
Here is the part of the script I'm having problems with:
time = Time.now #set time object to the current time
month = time.month #grab the number of the month from the
current
day = time.day #grab the day of the current time
year = time.year #grab the year of the current time
day = day - 14
def delete_old_files(dir, time)
Dir.chdir(dir) do
Dir.foreach(".") do |entry|
next if File.stat(entry).directory? #prevent from deleting
directories
#Use the modification time
if File.mtime(entry) < time
File.unlink(entry)
end
end
end
end
cleanup_dirs.each do |dir|
Find.find dir do |f|
File.unlink f if File.file? f &&
File.mtime(f) < limit
end
end
Cheers
robert
···
On 06.02.2008 23:16, grooveska wrote:
I am new to ruby and I'm having trouble with a script I'm working on.
It probably is something simple that I am missing. I am working on a
script to delete some files from several directories.
Here is the part of the script I'm having problems with:
time = Time.now #set time object to the current time
month = time.month #grab the number of the month from the
current
day = time.day #grab the day of the current time
year = time.year #grab the year of the current time
day = day - 14
def delete_old_files(dir, time)
Dir.chdir(dir) do
Dir.foreach(".") do |entry|
next if File.stat(entry).directory? #prevent from deleting
directories
#Use the modification time
if File.mtime(entry) < time
File.unlink(entry)
end
Here is the part of the script I'm having problems with:
time = Time.now #set time object to the current time
month = time.month #grab the number of the month from the
current
day = time.day #grab the day of the current time
year = time.year #grab the year of the current time
day = day - 14
def delete_old_files(dir, time)
Dir.chdir(dir) do
Dir.foreach(".") do |entry|
next if File.stat(entry).directory? #prevent from deleting
directories
#Use the modification time
if File.mtime(entry) < time
File.unlink(entry)
end
end
end
end
Well, for one thing, your day will be negative if it is less than 14.
I would think you'd be better off using Date instead of Time. To
illustrate (for today)...
On Feb 6, 2008 4:19 PM, grooveska <ryangs@mac.com> wrote:
I am new to ruby and I'm having trouble with a script I'm working on.
It probably is something simple that I am missing. I am working on a
script to delete some files from several directories.
Here is the part of the script I'm having problems with:
time = Time.now #set time object to the current time
month = time.month #grab the number of the month from the
current
day = time.day #grab the day of the current time
year = time.year #grab the year of the current time
day = day - 14
def delete_old_files(dir, time)
Dir.chdir(dir) do
Dir.foreach(".") do |entry|
next if File.stat(entry).directory? #prevent from deleting
directories
#Use the modification time
if File.mtime(entry) < time
File.unlink(entry)
end
end
end
end