Failed in opening a File in a directory in Ruby

I am trying to change the permissions of a file in the below given way.

File.chmod(0777,"util\logger\Sample.txt"). But the issue is the Sample.txt is in given path util/logger/Sample.txt.

The above operations is failing because unable to find the file name Sample.txt. I tried in this way Dir.chdir("\util\logger") and then im doing File.chmod(0777,"Sample.txt").

So again i need to get back to my base folder to do other operations. I felt little bad about this way of developing code.

So any help in accessing directly a file from a directory with out changing the current directory will greatly help me.

Thanks in advance, Aditya

Upvotes: 0

Views: 88

Answers (2)

I resolved it this way ..

File.chmod(0777,"util/logger/Sample.txt") , Previously i try to enter /util/logger

Upvotes: 0

Niels B.
Niels B.

Reputation: 6310

First of all, I don't think you can use backslash as a file separator on Unix systems. Backslash in paths is a Microsoft thing. Fortunately, Ruby has a nice way of removing file seperators from paths, so they can be calculated runtime depending on the environment.

Use File.join('path', 'to', 'file.txt')

This will return a string with the value path/to/file.txt on Unix.

Upvotes: 2

Related Questions