Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2391 > unrolled thread

Pathname: moving files & directories

Started bySimon Harrison <simon@simonharrison.net>
First post2011-04-06 13:18 -0500
Last post2011-04-06 15:09 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  Pathname: moving files & directories Simon Harrison <simon@simonharrison.net> - 2011-04-06 13:18 -0500
    Re: Pathname: moving files & directories Robert Klemme <shortcutter@googlemail.com> - 2011-04-06 21:07 +0200
    Re: Pathname: moving files & directories Jeremy Bopp <jeremy@bopp.net> - 2011-04-06 14:26 -0500
    Re: Pathname: moving files & directories Simon Harrison <simon@simonharrison.net> - 2011-04-06 15:09 -0500

#2391 — Pathname: moving files & directories

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-06 13:18 -0500
SubjectPathname: moving files & directories
Message-ID<b2265767440a64a48853a41cea71fe1a@ruby-forum.com>
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

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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#2394

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-06 21:07 +0200
Message-ID<903s05Flu0U1@mid.individual.net>
In reply to#2391
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/

[toc] | [prev] | [next] | [standalone]


#2397

FromJeremy Bopp <jeremy@bopp.net>
Date2011-04-06 14:26 -0500
Message-ID<4D9CBE52.2050905@bopp.net>
In reply to#2391
On 4/6/2011 13: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
> 
> 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:

1. Make a copy of a representative sample of the data with which you'll
experiment during development.
2. 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.

-Jeremy

[toc] | [prev] | [next] | [standalone]


#2403

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-06 15:09 -0500
Message-ID<fd88724318e82cd45bfd8b2194ae6e02@ruby-forum.com>
In reply to#2391
Thanks to both of you. As suggested I'll copy the files and experiment 
at home.

Cheers

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web