BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Tags -- Code
BradTrupp.com -- Tags -- Code -- Copy all files in a directory tree to a common destination. (Ruby)

Copy all files in a directory tree to a common destination. (Ruby)
(2008/10/08)

Most podcast receiver programs keep each feed in separate subdirectories. I was using this Ruby utility to flatten out all the files into a single directory before I copy them to my mp3 player. I actually use Yapcaster now which writes all the files out to a single directory making this utility obsolete for my particular purpose.

The ftools library makes available several class methods to the File class, for copying, moving, deleting, installing, and comparing files, as well as creating a directory path.

You could also use FileUtils instead as it contains the same functionality.

#!/usr/bin/ruby -w
require 'ftools'

pathIn = 'C:/Personal/Ruby/temp-in'
pathOut = 'C:/Personal/Ruby/temp-out'

#----------------------------------------------------------------------
# Pass 0 - Get file list
#----------------------------------------------------------------------

theFileList = [];
Dir[pathIn + '/**/*.*'].each do |fnn|
theFileList << fnn;
end;

#----------------------------------------------------------------------
# Pass 1 - extract name and copy file
#----------------------------------------------------------------------

theFileList.each do |f|
fni = f.reverse;
ii = fni.index('/')
if ii > 0 then
fnn = fni[0,ii]
fnn = fnn.reverse
else
fnn = f
end
fno = pathOut + '/' + fnn
puts "#{f} -> #{fno}"
File.copy(f, fno)
end;

#----------------------------------------------------------------------
# All Done....
#----------------------------------------------------------------------

puts 'press return to continue...'
gets

Tags: Code

Share: Del.icio.us | Digg | Facebook | Google Bookmarks | Reddit | Technorati | Twitter | Windows Live | Yahoo! My Web

View Comments (2)


 

Tag: Code
A Little Bit of Automation for your Backups (2010/02/25)
A Simple CSV to iCalendar Conversion Utility written in Ruby (2009/06/29)
A Simple iCalendar to Task Coach Conversion Utility written in Ruby (2009/05/09)
Using Ruby to generate Social Bookmarks for your Web Pages (2008/11/23)
Using Ruby to fix id3 tags on mp3 files (Ruby) (2008/11/10)
Copy all files in a directory tree to a common destination. (Ruby) (2008/10/08)
Check for File Changes using Ruby and MD5 (Ruby) (2008/08/20)
Create Checkpoints using Ruby and MD5 (Ruby) (2008/07/30)
Bash - A Basic Greeting Program. (Bash) (2007/10/31)
Bash - An Introduction to Scripting (Bash) (2007/10/10)
UUMerge Command Line Text File Merge (Delphi) (2007/07/01)
Explorer-Style Fly-By Buttons (Delphi) (2006/01/14)
Handle-Free Checkboxes in a String Grid (Delphi) (2006/01/14)

All Tags
Business Tips (5)
Code (13)
Life Skills (1)
Music (2)
My 15 minutes (5)
Old Articles (39)
Photos (10)

Advertisement