views:

27

answers:

2

I am currently writing a quick script in Ruby that goes through all the contents of two folders, and returns a list of all the files that are not in either of the two folders. Currently what I am doing is storing the paths of all the files of each directory in an array:

Find.find(dir1) do |path|
  if File.file?(path)
    directory1_files << path # Add path to an array of file_paths for the 1st directory.
  end
end # I repeat the process for the second directory and store their paths in an array called directory2_files.

The problem I am having is that when I try and subtract the two arrays (larger array - small array) to get the remaining files, I get an empty array. Reason I get this is because the full paths are trying to be subtracted instead of just the basenames. Ex: ~/folder1/file.txt != ~/folder2/file.txt How can I find if a file with the same name is in two folders, and remove it from a list so the only files remaining are the ones not present in both folders?

A: 

Use File.basename(filename). This also works if "filename" is a File object.

Alkaline
This won't work with nested directories, i.e. `~dir1/file.txt` and `~dir2/subdir1/file.txt` will be considered having the same relative path, while they are do not.
Ihor Kaharlichenko
The path is ignored with File.basename(). It returns only the file name without its directory part. Both File.basename('~/dir1/file.txt') and File.basename('~/dir2/subdir1/file.txt') return 'file.txt'.
Alkaline
+1  A: 

Since you know the paths to both folders (i.e. dir1 and dir2) I'd suggest to to calculate paths relative to them, so that later you could compare relative paths (effectively ignoring the ~/folder1 part of the path). Use dictionaries to map between relative and absolute paths (so that you could remove them).

Something like this:

dir1 = '~/folder1'
directory1_files = {}

Find.find(dir1) do |path|
  if File.file?(path)
    relative_path = path[dir1.length, path.length]
    directory1_files[relative_path] = path
  end
end

Then, whenever you have directory1_files and directory2_files, compare their .keys in order to find the differences.

Ihor Kaharlichenko