Browse | Submit A New Snippet | Create A Package

 

File Joiner

Type:
Class
Category:
File Management
License:
GNU General Public License
Language:
Ruby
 
Description:
File Joiner: Join all your "piece" files into a single file again

Versions Of This Snippet::

Rory O'Kane
Snippet ID Download Version Date Posted Author Delete
3341.0.12008-01-10 11:19Rory O'Kane
Changes since last version::
Re-indented to two spaces
1471.02006-04-17 05:52Saajan N

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0.1

# Joins all .piece files (eg. abc.ext.piece1, abc.ext.piece2 etc) in the current directory and creates a single file (eg. abc.ext)
# Does not do any error handling!
# Usage: ruby file_joiner.rb

class FileJoiner
  def initialize
    # sort the .piece filenames in the correct order i.e. if there are 10 .piece files in the current directory
    # we need to ensure that abc.ext.piece2 (and not abc.ext.piece10) follows abc.ext.piece1
    @pieces = Dir["*.piece*"].sort do |f1, f2|
          f1.split("piece").pop.to_i <=> f2.split("piece").pop.to_i
        end
    
    joined_filename = @pieces[0].split("\.")[0..1].join("\.")
    @joined_file = File.open(joined_filename, "wb")
  end
  
  def join
    @pieces.each { |file| @joined_file << File.open(file, "rb").read }
    @joined_file.close
  end
end

FileJoiner.new.join
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..