Delete multiple files in RAILS_ROOT/tmp/pdfs/

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

Dir[mask] # Class: Dir (Ruby 3.1.2) File.delete *files_matched_by_glob

That worked great. Here's how my code looks now.

Thanks,

Frank

Cleaned up code:

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

Cool! Much cleaner!

Thanks,

Frank

Conrad Taylor wrote:

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