require 'fileutils'
# Copy a file
FileUtils.cp 'myfile.txt', 'myfile.txt.bak'
# Copy list of files to folder
FileUtils.cp %w(somefile.txt another.txt), './bak'
FileUtils.cp Dir.glob('*.rb'), './bak', :verbose => true
my_dir = Dir["C:/Documents and Settings/user/Desktop/originalfiles/*.doc"]
my_dir.each do |filename|
name = File.basename('filename', '.doc')[0,4]
dest_folder = "C:/Documents and Settings/user/Desktop/destinationfolder/#{name}/"
FileUtils.cp(filename, dest_folder)
end
file = File.open('text.txt', 'r') # Opening file
copied_file = File.open('new_text.txt', 'w') # Creating new file
copied_file.puts(file.read) # Copying file
# Closing files
file.close
copied_file.close
# Testing
copied_file = File.open('new_text.txt', 'r') # Opening copied file
puts(copied_file.read)
# => What markets are called black? Those in Africa!