Copying a file from one directory to another with Ruby
I had to copy 1 in every 3 files from multiple directories to another. For those who wonder, this is how I did it:
require 'fileutils'
# Print origin folder question
puts 'Please select origin folder'
# Select origin folder
origin_folder = gets.chomp
# Select every file inside origin folder with .png extension
origin_folder = Dir["#{origin_folder}/*png"]
# Print destination folder question
puts 'Please select destination folder'
# Select destination folder
destination_folder = gets.chomp
# Select 1 in every 3 files in origin folder
(0..origin_folder.length).step(3).each do |index|
# Copy files
FileUtils.cp(origin_folder[index], destination_folder)
end
Something like this should work.
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
You have to actually specify the destination folder, I don't think you can use wildcards.
*
is a wildcard meaning "any number of characters", so "****
" means "any number of any number of any number of any number of characters", which is probably not what you mean.
?
is the proper symbol for "any character in this position", so "????
" means "a string of four characters only".