I can’t really experiment at work because the IT folk would probably not
be best pleased if I delete everything. I’m not sure what to use. I can
get the paths easily with Dir.glob and then grep for ‘obsolete’. But,
I’ve no idea how to rename the paths. Any help appreciated.
base = Pathname(’/newdir/obsolete’)
old_dirs = Set.new
Pathname.glob(‘dir//obsolete/’).each do |pn|
dir, file = pn.split
customer = dir.parent.basename
target_dir = base + customer
target_dir.directoy? or target_dir.mkdir
target = target_dir + file
FileUtils.mv pn, target
old_dirs << dir
end
old_dirs.each {|d| Dir.delete d}
I can’t really experiment at work because the IT folk would probably not
be best pleased if I delete everything. I’m not sure what to use. I can
get the paths easily with Dir.glob and then grep for ‘obsolete’. But,
I’ve no idea how to rename the paths. Any help appreciated.
Why can’t you experiment at work? You don’t need to use real paths or
you can omit the final move and delete operations. You need to test
your code anyway. Can’t you do it on your local machine or a virtual
machine?
I can’t really experiment at work because the IT folk would probably not
be best pleased if I delete everything. I’m not sure what to use. I can
get the paths easily with Dir.glob and then grep for ‘obsolete’. But,
I’ve no idea how to rename the paths. Any help appreciated.
First, a couple of recommendations given your concerns:
Make a copy of a representative sample of the data with which you’ll
experiment during development.
Design your solution to only copy the data to the new location while
leaving the original data in place.
This way you avoid the risk of trashing critical data while developing
and running your solution. Removal of the data from the old location
can be handled later, once the copy operation has been verified.
You’re on the right track with using Dir.glob to find your working set
of paths. Next, would be to use something like a regexp to chop up your
paths into something that you can reorder as you please. Here is
something to get you going:
require ‘fileutils’
src_paths = Dir.glob('dir//obsolete’)
dst_paths = src_paths.map do |path|
path.sub(%r[^dir/(.?)/obsolete], ‘/newdir/obsolete/\1’)
end
src_paths.zip(dst_paths).each do |src, dst|
puts “Copying #{src} -> #{dst}”
Uncomment this when you want to try the copy operation.
#FileUtils.cp_r(src, dst)
end
The above is untested and probably doesn’t consider all the corner cases
well enough, but it should be a reasonable starting point.