Deleting directories matching a pattern

I cannot believe I’m having trouble with this - it should be utterly
trivial, but I just can’t get Ruby to work correctly on this.

I’ve got a directory with subdirectories that have names such as
“Eb97503.120822”, where 97503 is a job submission number, and 120822 is
a time-date stamp. So the directory contents might look like this:

Eb97503.120622
Eb97504.120622
Eb98537.120722
Eb99659.120822

I want to pattern match the job submission number, and delete the
directories. I must have tried ten different ways of doing it using
strings set with the job submission number, but I just can’t get it to
work.

How the heck do I delete the middle two directories, once I have strings
set as:

job1 = 97504
job2 = 98537

This is embarrassing…

Thomas L. wrote in post #1073026:

[…] How the heck do I delete the middle two directories, once I have strings
set as:

job1 = 97504
job2 = 98537

This is embarrassing…

Would something like this work for you:

require ‘fileutils’
job = ARGV.shift.to_i

dir_pattern = “Eb%05d.*” % job
Dir[dir_pattern].each do |dir_name|
puts “Deleting #{dir_name}”
FileUtils.rm_r dir_name
end

On 8/24/2012 2:10 AM, Thomas L. wrote:

How the heck do I delete the middle two directories, once I have strings
set as:

job1 = 97504
job2 = 98537

This is embarrassing…
it’s pretty strait forward… loop all over the directories to match any
of the jobs.
it’s better you will use some array\hash and then read each directory
name.
then iterate over then and match them.
if the Eb is static prefix you can use the next psudo:

match the current directory name to the job number
hint: “dir_name”.start_with? (“Eb” + job + “.”)
and in a match case erase the directory and erase the job from the todo
list.
an exit mark can be then end of the directories list with a log of
unfind jobs that was needed to be erased or…
the jobs to erase size is 0.

Good luck,
Eliezer

what code do you have so far?

On Fri, Aug 24, 2012 at 4:59 AM, Jim W. [email protected]
wrote:

require ‘fileutils’
job = ARGV.shift.to_i

dir_pattern = “Eb%05d.*” % job
Dir[dir_pattern].each do |dir_name|
puts “Deleting #{dir_name}”
FileUtils.rm_r dir_name
end

I offer

require ‘fileutils’

FileUtils.rm_r Dir[ARGV.map {|job| "base/Eb#{job}."}]

Kind regards

robert