5

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
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Sven
  • 53
  • 1
  • 3
  • 3
    With 1.9.3 at least, you can just use forward slashes and you don't have to worry about escaping or anything `Dir['//server/share/*']`. Looks a heck of a lot nicer than having to escape all of the backslashes. –  Dec 06 '12 at 20:02
  • I ran into the problem of trying to use UNC-paths: [UNC not having access (last comment)](https://stackoverflow.com/questions/37319077/wmi-access-to-unc-paths) – Henrik Carlsen May 05 '21 at 11:30

3 Answers3

7

Try to escape '\' with another '\'

Dir.entries('\\\\\\\\192.168.10.1\\\\share')
dee-see
  • 23,668
  • 5
  • 58
  • 91
ILog
  • 321
  • 2
  • 10
  • 2
    Oh lord. I had a typo in my path... However, it works with double backslashes. Thanks everyone... – Sven Oct 22 '10 at 10:11
6

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
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
0

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:

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Doesn't work either when doubling the backslashes: irb(main):021:0> Dir.entries('\\\\server\\share\\') Errno::ENOENT: No such file or directory - \\server\share\ – Sven Oct 22 '10 at 10:01