Posted inRuby

How to Check If a File Exists in Ruby?

here simple Checking whether a ruby check if file exists or not, is one of the most basic file related project which you will do often How to Delete a File in Ruby? and thankfully Ruby’s File class supports two methods called File.file?() and File.exist?() to simple test file existence. The only main this two methods difference between two ways is File.exist?() will also return only Boolean value like as a true for directories.

Ruby – Check whether a file or directory exists

The File.exist?() the function checks whether or not a file or folder exists. This function returns only Boolean value like as a TRUE if the file or folder exists, otherwise, it returns FALSE.

check directory or file existence

#Ruby function to check folder or file existence
if(File.exist?('Hello.rb')) 
  puts 'file or folder exists'
else 
  puts 'file or folder not found'
end

The File.file?() function checks whether or not a file exists. This function returns only Boolean value like as a TRUE if the file exists, otherwise it returns FALSE.

check file existence

#Ruby function to check file existence
if(File.file?('hello.rb')) 
    puts 'file exists'
else 
    puts 'file not found'
end

Also Read: How to Delete a File in Ruby?

Apart from the above methods Ruby also provides File.directory?() method to check whether a folder exists or not. This function returns only Boolean value like as a TRUE if the folder exists, otherwise, it returns FALSE.

check directory existence

#Ruby function to check folder existence
if(File.directory?('pakainfo')) 
  puts 'folder exists'
else
  puts 'folder not found'
end

As of now, you have seen a file and folder existence checking with Ruby’s File class ways as well as thankfully Ruby’s Dir class also offering method Dir.exist?() for checking folder existence. This function returns only Boolean value like as a TRUE if the folder exists, otherwise, it returns FALSE.

check directory existence

#Ruby function to check folder existence
if(Dir.exist?('pakainfo')) 
  puts 'folder exists'
else
  puts 'folder not found'
end

Ruby: Check whether a file or directory exists

In Ruby, you can check whether a file or directory exists using the File.exist? method. Here’s an example:

if File.exist?("path/to/file_or_directory")
  puts "File or directory exists"
else
  puts "File or directory does not exist"
end

In the code above, replace “path/to/file_or_directory” with the actual path to the file or directory you want to check.

If the file or directory exists, the File.exist? method will return true, and the code will print “File or directory exists”. Otherwise, it will return false, and the code will print “File or directory does not exist”.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype