Maile Thiesen
Maile Thiesen

Reputation: 131

RUBY - How to copy files to a specific directory if the file does not exist

I think this is a pretty simple question, but I'm stuck.

I'm trying to write a script to look in the first directory, then check a second directory to see if there are matching files. If the files do match, I need to copy the files from the second directory to a third directory, only if they do not already exist in the third directory.

Here's a copy of what I have so far. The code still overwrites the files in the third directory even if they already exist. What am I doing wrong?

Thanks!

#!/usr/bin/ruby

require 'FileUtils'

library_path_1 = ARGV[0]
library_path_2 = ARGV[1]
library_path_3 = ARGV[2]

dir_1 = Dir.glob(library_path_1 + "/**/*").select{ |x| File.file? x }
dir_2 = Dir.glob(library_path_2 + "/**/*").select{ |x| File.file? x }
destination = Dir.glob(library_path_3 + "/**/*").select{ |x| File.file? x }

dir_1.each do |filename|
  dir_2.each do |path|
    destination.each do |existing_file|

      existing_file = File.basename(existing_file)

      if path.include?(File.basename(filename))
        FileUtils.cp(path, library_path_3) unless File.exists?(existing_file)
      end

    end
  end
end

Upvotes: 5

Views: 3360

Answers (4)

Maile Thiesen
Maile Thiesen

Reputation: 131

Here's the final script that is working based on @Cary Swoveland's answer. Thanks all!

require 'FileUtils'

library_path_1 = ARGV[0]
library_path_2 = ARGV[1]
library_path_3 = ARGV[2]

# check the managed media folder for files, look in original capture scratch for all files in managed media.
# if files in managed media exist in original capture scratch, copy them to the destination folder.

managed_media_folder = Dir.glob(library_path_1 + "/**/*").select{ |x| File.file? x }
original_capture_scratch = Dir.glob(library_path_2 + "/**/*").select{ |x| File.file? x }
destination = Dir.glob(library_path_3 + "/**/*").select{ |x| File.file? x }

bn = managed_media_folder.map { |fn| File.basename fn } - destination.map { |fn| File.basename fn }
original_capture_scratch.each do |fn| 
  if bn.include? File.basename(fn)
    puts fn
    FileUtils.cp(fn, library_path_3) 
  end
end

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110675

bn = dir1.map { |fn| File.basename fn } - destination.map { |fn| File.basename fn }
dir2.each { |fn| FileUtils.cp(fn, library_path_3) if bn.include? File.basename(fn) }

Upvotes: 2

Maile Thiesen
Maile Thiesen

Reputation: 131

dir_1.each do |file|
  filename = File.basename(file)
  if exists_in_dir2?(filename) && exists_in_dir3?(filename) == false
    copy_to_destination(filename) 
  end
end

def copy_to_destination(filename)
    File.copy(filename, "#{library_path_3}/{filename}")
end

def exists_in_dir2?(filename)
    dir_2.each do |path|
        path.include?("#{filename}")
    end
end

def exists_in_dir3?(filename)
    destination.each do |existing_file|
        File.exist?("#{filename}")
    end
end

Upvotes: 1

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16507

It seems you are validating files by its just file names, but you have to check them by full name, like this:

existing_file = File.basename(existing_file)

if path.include?(File.basename(filename))
   FileUtils.cp(path, library_path_3) unless File.exist?(File.join(library_path_3, existing_file))
end

Upvotes: 0

Related Questions