如何在Powershell中获取Parent的父目录? [英] How to get the Parent's parent directory in Powershell?
本文介绍了如何在Powershell中获取Parent的父目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,如果我将目录存储在变量中,请说:
So if I have a directory stored in a variable, say:
$scriptPath = (Get-ScriptDirectory);
现在我想找到目录两个父级.
Now I would like to find the directory two parent levels up.
我需要一个好的方法:
$parentPath = Split-Path -parent $scriptPath
$rootPath = Split-Path -parent $parentPath
我可以在一行代码中到达 rootPath 吗?
Can I get to the rootPath in one line of code?
推荐答案
目录的版本
get-item
是您友好的帮手.
(get-item $scriptPath ).parent.parent
如果你只想要字符串
(get-item $scriptPath ).parent.parent.FullName
文件版本
如果 $scriptPath
指向一个文件,那么你必须先调用它的 Directory
属性,所以调用看起来像这样
Version for a file
If $scriptPath
points to a file then you have to call Directory
property on it first, so the call would look like this
(get-item $scriptPath).Directory.Parent.Parent.FullName
备注
这仅在 $scriptPath
存在时有效.否则,您必须使用 Split-Path
cmdlet.
Remarks
This will only work if $scriptPath
exists. Otherwise you have to use Split-Path
cmdlet.
这篇关于如何在Powershell中获取Parent的父目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文