3

I'm trying to check if a file exists with RoR but it keeps saying this file does not exist and I can't understand why.

if File.exist?('~/Desktop/test.xls')
  p 'File EXISTS'
else
  p 'Cannot find any file'
end

Of course the file exists and I can access it with the terminal using 'ls'.

The script is into the scripts folder of my rails app.

Thanks for any help

El-Cortez
  • 113
  • 1
  • 11
  • Have you tried replacing the `~` with your actual home directory? – fmt Sep 08 '16 at 14:57
  • Yes, thanks it just worked ! by any chance, would you know, then how I could share this code with non-programmers? I was hoping anyone could access the file with just one button but if I code my own name instead of '~' it won't be possible, right? – El-Cortez Sep 08 '16 at 15:07

1 Answers1

2

It should be something like this:

if File.exist?("#{Dir.home}/Desktop/test.xls")
  p 'File EXISTS'
else
  p 'Cannot find any file'
end

where Dir.home is your particular home directory (in my case it is /Users/m.pontyushenko/)

Maxim Pontyushenko
  • 2,983
  • 2
  • 25
  • 36
  • it works, thanks a lot ! by any chance, would you know, then how I could share this code with non-programmers? I was hoping anyone could access the file with just one button but if I code my own name instead of '~' it won't be possible, right? – El-Cortez Sep 08 '16 at 15:06
  • @El-Cortez you mean you want to check if a person has particular file on their computer? – Maxim Pontyushenko Sep 08 '16 at 15:07
  • @El-Cortez I made an update to my answer. Just check it. – Maxim Pontyushenko Sep 08 '16 at 15:09
  • Awesome, thanks so much! You just saved my afternoon^^ – El-Cortez Sep 08 '16 at 15:12
  • @El-Cortez I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers." – Maxim Pontyushenko Sep 08 '16 at 15:16
  • Just done it, sorry, I though it was the up arrow that was doing the job. Thanks again ! ^^ – El-Cortez Sep 08 '16 at 15:42
  • 1
    @El-Cortez Up arrow is something like "Like" in Facebook and you can upvote all the answers while "Accepted" means that this particular post answers your question/solve the problem described in question. – Maxim Pontyushenko Sep 08 '16 at 15:48