Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a filename is a folder or a file

Tags:

file

ruby

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?

like image 805
icn Avatar asked Apr 11 '12 23:04

icn


People also ask

How can I tell if a file is a folder or a file?

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.

How do you check if a file is a file in Python?

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 .


2 Answers

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

like image 177
thelazydeveloper Avatar answered Oct 13 '22 01:10

thelazydeveloper


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

like image 36
mbigras Avatar answered Oct 13 '22 02:10

mbigras