Reputation: 879
I am trying to find a matching list of folders on my C:/ drive and then execute some code but its not working as expected.
I can do it fine with a single folder but not sure how to get it working with a list of folders that I want to find.
My code
Dir.glob("C:/*")
directory_list = Array.new
directory_list << "FolderA"
directory_list << "FolderB"
if Dir.exists?(directory_list)
puts "Does exist"
else
puts "Does not Exist"
end
The following solution provided a proof of concept for me
dirs = ["FolderA", "FolderB"]
reg = Regexp.union dirs exists,
rest = Dir.glob("{B,C,D}:/*").partition{ |path| path =~ reg }
puts exists
With thanks to Kyle in the chat room.
Upvotes: 1
Views: 247
Reputation: 22268
On windows, the directories are prepended with the drive so you need to:
"C:/FolderB" =~ /FolderB|FolderA/
The code:
dirs = ["FolderA", "Folderb"]
reg = Regexp.union dirs
exists, rest = Dir.glob("C:/*").partition{ |path| path =~ reg }
# now you have two arrays, one of directories that exist and the rest
Upvotes: 1
Reputation: 2081
c_drive = Dir.glob("**/") %w(FolderA, FolderB).each do |dir| if c_drive.include?(dir) puts "#{dir} exists" else puts "#{dir} does not exist" end end
Upvotes: 1