# How to check the file is available or not

**URL:** <https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581>\
**Category:** Ruby\
**Created:** [May 14, 2010, 7:57am UTC](https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581 "2010-05-14T07:57:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 14, 2010, 7:57am UTC](https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581/1 "2010-05-14T07:57:14Z")

</div>

How to check the file is available or not.  
write this simple code for checking the file.  
but if file not found they print the first condition.

if(contents = File.read(“c:/test/test.txt”))  
log.debug “Text File Available…!!!”  
else  
log.debug “File not Available”  
break  
end

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 14, 2010, 9:29am UTC](https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581/2 "2010-05-14T09:29:49Z")

</div>

On Fri, May 14, 2010 at 7:57 AM, KingMaker KingMaker  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```

if test ?e, “c:/test/test.txt”

Jesus.

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 14, 2010, 10:41am UTC](https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581/3 "2010-05-14T10:41:45Z")

</div>

KingMaker KingMaker wrote:

> How to check the file is available or not.

You can do this with File.exist?("…"), but I’d do this:

begin  
contents = File.read(“c:/test/test.txt”)  
log.debug “Text File Available…!!!”  
rescue Errno::ENOENT  
log.debug “File not available”  
break  
end

This is technically better because it avoids a race condition (the file  
being deleted within the short time between when you test for its  
existence, and when you actually read it)

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 15, 2010, 12:09am UTC](https://www.ruby-forum.com/t/how-to-check-the-file-is-available-or-not/188581/4 "2010-05-15T00:09:12Z")

</div>

KingMaker KingMaker wrote:

> How to check the file is available or not.

There is File.readable?(name)  
but who knows if it actually works in windows…if you’re not on windows  
it probably works fine.

-rp
