简体   繁体   English

替代连接路径来处理不存在的驱动器

[英]Alternative to join-path to handle nonexistent drive

I made a function that creates a file name.我做了一个创建文件名的函数。

The function breaks when sent a drive letter that doesn't exist.当发送一个不存在的驱动器号时,该功能会中断。

I have have changed the function to not use join-path but I am wondering if there is another way?我已将函数更改为不使用 join-path 但我想知道是否还有其他方法?

The try / catch is not working as expected. try / catch 未按预期工作。

Perhaps the best answer is not using join-path ?也许最好的答案是不使用 join-path ?

$fullPath = join-path -path $DirectoryName  -childpath ((Get-Date).ToString('yyyy_MM_ddThhmmss-') + ($ReportName + $Extension)) -ErrorAction SilentlyContinue

To emulate Join-Path 's behavior without the - surprising and vexing - requirement that a drive spec.模拟Join-Path的行为,而没有驱动器规范的令人惊讶和恼人的要求。 (eg, N: ) refer to an existing drive (even though no other components are required to exist): (例如, N: )指的是现有驱动器(即使不需要其他组件存在):

Pragmatically speaking, you can simply use string concatenation / interpolation to join your path components:务实地说,您可以简单地使用字符串连接/插值来连接您的路径组件:

$dir = 'N:\foo'; $file = 'bar'

# String concatenation
$joinedPath = $dir + '\' + $file

# String interpolation
$joinedPath = "$dir\$file"

# With an open-ended number of path components.
$components = $dir, 'sub', $file
$joinedPath = $components -join '\'

While, depending on the input components, this can result in accidentally duplicated path separators (eg, N:\\foo\\\\bar ), this is usually not a problem, because the file-system APIs treat such duplicates as a single separator;虽然根据输入组件,这可能会导致意外重复的路径分隔符(例如, N:\\foo\\\\bar ),但这通常不是问题,因为文件系统 API 将此类重复视为单个分隔符; that is, N:\\foo\\\\bar is treated the same as N:\\foo\\bar .也就是说, N:\\foo\\\\bar被视为与N:\\foo\\bar

If you want to avoid duplicated separators, see the solution in the next section.如果您想避免重复的分隔符,请参阅下一节中的解决方案。


To more fully emulate Join-Path , which involves removing accidentally duplicated separators and normalizing them to the platform-native form (eg, on Windows, / -> \\ ), use the following:要更全面地模拟Join-Path ,这涉及删除意外重复的分隔符并将它们规范化为平台原生形式(例如,在 Windows 上, / -> \\ ),请使用以下内容:

$dir = 'N:\foo'; $file = 'bar.txt'
$components = $dir, $file

$joinedPath = 
  ($components -join '\') -replace '(?!^)([\\/])+', [IO.Path]::DirectorySeparatorChar

To wrap this up in a (very simple) function, join-AnyPath :要将其封装在一个(非常简单的)函数中, join-AnyPath

function join-AnyPath {
  ($args -join '\') -replace '(?!^)([\\/])+', [IO.Path]::DirectorySeparatorChar
}

# Sample call
join-AnyPath N:\dir\ /foo/ bar.txt

The above yields N:\\dir\\foo\\bar.txt (on Windows).以上产生N:\\dir\\foo\\bar.txt (在 Windows 上)。


Note: Theo points out that there's also the [IO.Path]::Combine() .NET method, but while it doesn't require that a drive exist either, its behavior differs from Join-Path with respect to how \\ -prefixed components are handled, which can lead to surprising results: see this answer .注意: Theo指出还有[IO.Path]::Combine() .NET 方法,但虽然它也不需要驱动器存在,但它的行为与Join-Path不同在于\\前缀的方式组件被处理,这可能会导致令人惊讶的结果:请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM