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
On Jan 5, 12:26Â pm, Frank K. [email protected] wrote:
Any suggestions are appreciated.
Thanks,
Frank
Posted viahttp://www.ruby-forum.com/.
Dir[mask] # class Dir - RDoc Documentation
File.delete *files_matched_by_glob
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] # class Dir - RDoc Documentation
File.delete *files_matched_by_glob
On Tue, Jan 5, 2010 at 9:26 AM, Frank K. [email protected] wrote:
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
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 K. 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
On Jan 5, 1:47Â pm, Frank K. [email protected] wrote:
Frank K. wrote:
–
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