开发者

How do I address a UNC path in Ruby on Windows?

开发者 https://www.devze.com 2023-01-21 16:53 出处:网络
I\'m trying to access a UNC share via irb on Windows. In the Windows shell it would be \\\\server\\share

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


Try to escape '\' with another '\'

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


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:

0

精彩评论

暂无评论...
验证码 换一张
取 消