On Thu, Apr 17, 2008 at 4:53 AM, Clement Ow
<clement.ow / asia.bnpparibas.com> wrote:
> Pardon for my multiple posts in this forum as Im rushing a project but
>  still quite new in ruby..
>
>  Anyway, I would like to select files to delete based on their
>  datestamped folders.
>  For example, I would like to delete files which older than 5 business
>  days (i.e that means you dont count sats and suns). Currently my code
>  looks like this:
>  def delFiles
>  sd_a=$del_path.zip($del_selection)
>  sd_a.each do |sd|
>   $del_path, $del_selection = sd
>   del = File.join $del_path, $del_selection
>   puts "Files/Folders Deleted: #{del}"
>   FileUtils.rm_r Dir.glob(del)
>  end #each
>   end #delFiles
>
>  Im actually using arrays to contain my del_path and del_selection
>  because I will need to delete multiple directories or files all at once.
>  Is there any way where I can build on my code but with the criteria that
>  only files older than 5 business days (bearing in mind that files not
>  only contain the date eg. 20080331 but can also come with other
>  characters eg. risk20080331) get deleted.
>  Much help is really appreciated. ;)

If you mean that the date is part of the name, I have a script that
does something
similar. Let me paste you the relevant parts (this is not a complete program,
it's just the part that makes the checks against the dates).

Here I receive in params the list of folders to process,
and the age of files for zipping and deleting. So for example
to say: delete files older than 15 days, zip files older than 7 days
I pass delete=15, zip=7. In this part I calculate the dates to
check:

    folders = params[:directories].values
    have_to_zip = params[:zip].given?
    zip = params[:zip].value
    if have_to_zip
      zip_date = DateTime.now - zip
    end
    delete = params[:delete].value
    delete_date = DateTime.now - delete

Now I build a regexp to match the file names and extract the date from them:

    regexp = Regexp.compile(/^(\d\d\d\d-\d\d-\d\d).*\.log(\.gz)?$/)

Now I traverse the folders, trying to match the filenames against the regexp.
When I find a match, I check the date in the name against the delete date
and the zip date and act accordingly, storing info for a report:

    fileData = Struct.new(:name, :size)
    deleted_files = []
    zipped_files = []

    folders.each do |folder|
      Find.find(folder + "/") do |file|
        match = regexp.match(File.basename(file));
        if match
          file_date = DateTime.parse(match[1])
          size = File.stat(file).size
          if delete_date > file_date
            deleted_files << fileData.new(file,size)
            File.delete(file)
          elsif have_to_zip && zip_date > file_date && !match[2]
            zipped_files << fileData.new(file,size)
            `gzip -f #{file}`
          end
        end
      end
    end


If you want to check the actual modification date of the file,
I think File.stat can help on that, or I don't know if File.find has
an option to search
for files based on date.

Hope this helps.

Jesus.