Check if the file name is a folder or file

I have a small piece of Ruby code:

files.each do |file| FileUtils.mkdir_p(File.dirname(target)) FileUtils.cp_r(file, target, :verbose => true) end 

I would like to add a check, for example

 if file is a folder # do this if file is a file # do that 

How to implement Ruby?

+46
ruby file
Apr 11 2018-12-12T00:
source share
2 answers

You can use File.directory?("name") and / or File.file?("name") .

+91
Apr 11 2018-12-12T00:
source share

Also good to check Pathname#directory? and Pathname#file?

+1
Jan 12 '17 at 6:28
source share



All Articles