Check if a filename is a folder or a file

RubyFile

Ruby Problem Overview


I have a little 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 like

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

How do I implement in Ruby?

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

Also a good idea to check out Pathname#directory? and Pathname#file?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionicnView Question on Stackoverflow
Solution 1 - RubythelazydeveloperView Answer on Stackoverflow
Solution 2 - RubymbigrasView Answer on Stackoverflow