powershell:Join-Path连接多级子目录的方法

我们知道Join-Path可以用来创建路径,比如

Join-Path 'C:\Program Files' WindowsPowerShell

会把C:\Program Files和子文件/文件夹WindowsPowerShell连接在一起生成 C:\Program Files\WindowsPowerShell

但根据Join-Path的说明,其并不支持将多级子文件夹连接在一起生成一个新路径。
比如,我想将C:\Program Files 以及WindowsPowerShellModules两级子目录连接生成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?》

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值