我想将以下PATH转换为Ruby中的UNC路径。
C:/Users/bla/bla2/asdf-ut-script.js
答案 0 :(得分:1)
UNC path要求您知道服务器和共享的名称,路径中不存在这两个名称,除非您正在寻找以下内容:
\\localhost\C$\Users\bla\bla2\asdf-ut-script.js
如果这是你想要的:
def File.to_unc( path, server="localhost", share=nil )
parts = path.split(File::SEPARATOR)
parts.shift while parts.first.empty?
if share
parts.unshift share
else
# Assumes the drive will always be a single letter up front
parts[0] = "#{parts[0][0,1]}$"
end
parts.unshift server
"\\\\#{parts.join('\\')}"
end
puts File.to_unc( "C:/Users/bla/bla2/asdf-ut-script.js" )
#=> \\localhost\C$\Users\bla\bla2\asdf-ut-script.js
puts File.to_unc( "C:/Users/bla/bla2/asdf-ut-script.js", 'filepile' )
#=> \\filepile\C$\Users\bla\bla2\asdf-ut-script.js
puts File.to_unc( "/bla/bla2/asdf-ut-script.js", 'filepile', 'HOME' )
#=> \\filepile\HOME\bla\bla2\asdf-ut-script.js