Question

I'm trying to access a UNC share via irb on Windows. In the Windows shell it would be

\\server\share

I tried escaping all of the backslashes.

irb(main):016:0> Dir.entries '\\\\server\share'
Errno::ENOENT: No such file or directory - \\server\share

and using the IP address instead of the name

irb(main):017:0> Dir.entries '\\\\192.168.10.1\share'
Errno::ENOENT: No such file or directory - \\192.168.10.1\share
Was it helpful?

Solution

Try to escape '\' with another '\'

Dir.entries('\\\\192.168.10.1\\share')

OTHER TIPS

Ruby interprets paths in a POSIX way, meaning you should use forward slashes when possible.

//server/share

The trailing slash is unnecessary, just like in native Windows. You can use backslashes, but they have to be escaped with another backslash.

\\\\server\\share

I'd only recommend that when you're passing UNC paths from native programs directly and can't transform them. When I'm mixing Ruby/Windows paths, like in a build script that uses Ruby methods and native Windows apps, which each require different paths, I'll use some helpers:

def windows_path(value)
  value.gsub '/', '\\'
end

def posix_path(value)
  value.gsub '\\', '/'
end

Always enclose your paths in single quotes, if they're literal, or double-quotes if you're interpolating. Forward slashes tell Ruby to start interpreting a regex. This is a common error for me in irb.

irb> File.exists? //server/share
SyntaxError: (irb):2: unknown regexp options - rvr

Looks like you're missing the trailing slash. Try '\\server\share\'

It's similar to the root directory of a Windows drive. That's C:\, not C:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top