Whats the best way to delete multiple files with a given prefix in the ''tmp'' folder of a RAILS application? File.delete works great if I have just one file and I know what the file name is. But I''m creating PDF documents as ''user_name_some_report_name_yyyymmddhhmmss.pdf'' I''d like to use something like ''File.delete("#{RAILS_ROOT}/tmp/pdfs/user_name_some_report_name*")''. Any suggestions are appreciated. Thanks, Frank -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 5, 12:26 pm, Frank Kany <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Whats the best way to delete multiple files with a given prefix in the > ''tmp'' folder of a RAILS application? > > File.delete works great if I have just one file and I know what the file > name is. But I''m creating PDF documents as > ''user_name_some_report_name_yyyymmddhhmmss.pdf'' > > I''d like to use something like > ''File.delete("#{RAILS_ROOT}/tmp/pdfs/user_name_some_report_name*")''. > > Any suggestions are appreciated. > > Thanks, > > Frank > -- > Posted viahttp://www.ruby-forum.com/.Dir[mask] # http://ruby-doc.org/core/classes/Dir.html#M002323 File.delete *files_matched_by_glob -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That worked great. Here''s how my code looks now. Thanks, Frank -------------- @user_files_mask = File.join("#{RAILS_ROOT}/tmp/pdfs/#{User.find(current_account.id).full_name}*") @user_files = Dir.glob(@user_files_mask) @user_files.each do |file_location| File.delete(file_location) end -------------- pharrington wrote:> Dir[mask] # http://ruby-doc.org/core/classes/Dir.html#M002323 > File.delete *files_matched_by_glob-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Cleaned up code: ------------ @user_files = Dir.glob(File.join("#{RAILS_ROOT}/tmp/pdfs/#{User.find(current_account.id).full_name}*")) @user_files.each do |file_location| File.delete(file_location) end ------------- Frank Kany wrote:> -------------- > @user_files_mask = > File.join("#{RAILS_ROOT}/tmp/pdfs/#{User.find(current_account.id).full_name}*") > > @user_files = Dir.glob(@user_files_mask) > > @user_files.each do |file_location| > File.delete(file_location) > end > ---------------- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Jan 5, 2010 at 9:26 AM, Frank Kany <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Whats the best way to delete multiple files with a given prefix in the > ''tmp'' folder of a RAILS application? > > File.delete works great if I have just one file and I know what the file > name is. But I''m creating PDF documents as > ''user_name_some_report_name_yyyymmddhhmmss.pdf'' > > I''d like to use something like > ''File.delete("#{RAILS_ROOT}/tmp/pdfs/user_name_some_report_name*")''. > > Any suggestions are appreciated. > > Thanks, > > Frank >Frank, you can do something like this: require ''fileutils'' FileUtils.rm_rf Dir[ "#{RAILS_ROOT}/tmp/pdfs/user_name_some_report_name*" ] Good luck, -Conrad> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Cool! Much cleaner! Thanks, Frank Conrad Taylor wrote:> Frank, you can do something like this: > > require ''fileutils'' > > FileUtils.rm_rf Dir[ > "#{RAILS_ROOT}/tmp/pdfs/user_name_some_report_name*" ] > > Good luck, > > -Conrad-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 5, 1:47 pm, Frank Kany <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Cleaned up code: > ------------ > @user_files > Dir.glob(File.join("#{RAILS_ROOT}/tmp/pdfs/#{User.find(current_account.id). full_name}*")) > > @user_files.each do |file_location| > File.delete(file_location) > end > ------------- > > Frank Kany wrote: > > -------------- > > @user_files_mask > > File.join("#{RAILS_ROOT}/tmp/pdfs/#{User.find(current_account.id).full_name }*") > > > @user_files = Dir.glob(@user_files_mask) > > > @user_files.each do |file_location| > > File.delete(file_location) > > end > > -------------- > > -- > Posted viahttp://www.ruby-forum.com/.Three issues: Why are you using instance variables here? The file mask and list of files seem to be of use only to the method in context, so just use local variables... The File.join here is useless; File.join concatenates each argument passed to it with the directory separator; it does nothing to the individual arguments. You probably wanted to pass each part of the pathname as separate arguments, without any directory separator. You do not need to iterate through each of user_files; File.delete deletes each filename passed to it. Just use File.delete *user_files -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.