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?
File. isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.
To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .
You can use File.directory?("name")
and/or File.file?("name")
.
Also a good idea to check out Pathname#directory?
and Pathname#file?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With