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?
Since you know the paths to both folders (i.e.
dir1anddir2) I’d suggest to to calculate paths relative to them, so that later you could compare relative paths (effectively ignoring the~/folder1part of the path). Use dictionaries to map between relative and absolute paths (so that you could remove them).Something like this:
Then, whenever you have
directory1_filesanddirectory2_files, compare their.keysin order to find the differences.