我们知道Join-Path可以用来创建路径,比如
Join-Path 'C:\Program Files' WindowsPowerShell
会把C:\Program Files和子文件/文件夹WindowsPowerShell连接在一起生成 C:\Program Files\WindowsPowerShell
但根据Join-Path的说明,其并不支持将多级子文件夹连接在一起生成一个新路径。
比如,我想将C:\Program Files 以及WindowsPowerShell和Modules两级子目录连接生成C:\Program Files\WindowsPowerShell\Modules,单靠一条Join-Path调用是做不到的。
解决方法1:
# 管道连接的两次Join-Path调用实现多级子文目录连接
$Modules=Join-Path 'C:\Program Files' WindowsPowerShell | Join-Path -ChildPath Modules
$Modules
解决方法2:
# 以嵌套方式进行两次Join-Path调用实现多级子文目录连接
$Modules= Join-Path (Join-Path 'C:\Program Files' WindowsPowerShell) -ChildPath Modules
$Modules
解决方法3:
# 使用[io.path]::combine函数实现多级子文目录连接
$Modules=[io.path]::combine('C:\Program Files',"WindowsPowerShell","Modules")
$Modules
参考资料:
《Join-Path》
《How do I use join-path to combine more than two strings into a file path?》
本文介绍了在PowerShell中如何组合多级子文件夹路径的方法,包括使用Join-Path命令的不同方式以及利用[io.path]::combine函数实现路径的便捷组合。
112





