# If File.exist? does not work

**URL:** <https://www.ruby-forum.com/t/if-file-exist-does-not-work/77408>\
**Category:** Ruby\
**Created:** [March 6, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/if-file-exist-does-not-work/77408 "2006-03-06T19:44:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [March 6, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/if-file-exist-does-not-work/77408/1 "2006-03-06T19:44:06Z")

</div>

I got like:

if File.exist?  
“#{RAILS\_ROOT}/public/images/#{property.id}/middle/#{image.name}”  
File.unlink  
“#{RAILS\_ROOT}/public/images/#{property.id}/middle/#{image.name}”  
end

why it does not work ?

error  
No such file or directory -  
script/…/config/…/public/images/144/middle/cfa9a31932bea2904485b56294764a6b.jpg

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [March 6, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/if-file-exist-does-not-work/77408/2 "2006-03-06T22:14:04Z")

</div>

misiek wrote:

> error  
> No such file or directory -  
> script/…/config/…/public/images/144/middle/cfa9a31932bea2904485b56294764a6b.jpg

The snippet you posted works, assuming the wrapping is introduced by  
the post software.

Could there be a typo that is causing the ‘if’ to be treated as the end  
of a previous statement? This would also require a ‘begin’ to match  
the ‘end’…

cheers

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [March 6, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/if-file-exist-does-not-work/77408/3 "2006-03-06T22:57:04Z")

</div>

2006/3/6, misiek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> error  
> No such file or directory -  
> script/…/config/…/public/images/144/middle/cfa9a31932bea2904485b56294764a6b.jpg

Looks ok to me. But to avoid spelling errors I’d store the name in a  
variable like  
f = File.join RAILS\_ROOT, “public”, “images”, property.id, “middle”,  
image.name  
File.unlink f if File.exists? f

HTH

Kind regards

robert
