How to specify UNC path in Ruby on Windows?

I am trying to access a UNC resource through irbin Windows. In a Windows shell, it will be

\\server\share

I tried to avoid all the backslashes.

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

and using an IP address instead of a name

irb(main):017:0> Dir.entries '\\\\192.168.10.1\share'
Errno::ENOENT: No such file or directory - \\192.168.10.1\share
+3
source share
3 answers

Try to escape '\' with another '\'

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

+5
source

Ruby interprets paths in POSIX , that is, if possible, you should use slashes.

//server/share

, Windows. , .

\\\\server\\share

, UNC- . Ruby/Windows, , script, Ruby Windows, , :

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

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

, , . Ruby, . irb.

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

, . '\\server\share\'

Windows. C:\, C:

0
source

Source: https://habr.com/ru/post/1770884/


All Articles