From: Robert Klemme Date: 2011-04-07T04:10:59+09:00 Subject: Re: Pathname: moving files & directories On 06.04.2011 20:18, Simon Harrison wrote: > This came up at work a while ago and our IT dept. said it was > impossible. If we have the following paths: > > dir/customer1/file1 > dir/customer1/file2 > dir/customer1/lots more files > dir/customer2/file1 > dir/customer2/file2 > dir/customer2/file3 > dir/customer2/obsolete/file1 > dir/customer2/oblolete/file2 > dir/customer3/file1 > dir/customer3/file2 > dir/customer3/obsolete/file1 > > .etc > > how can we end up with this: > > /newdir/obsolete/customer2/file1 > /newdir/obsolete/customer2/file2 > /newdir/obsolete/customer3/file1 > > .etc Roughly something like this: # untested require 'pathname' require 'fileutils' require 'set' 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? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/