将最新文件过滤到变量 $latestfile 中后,我需要 $latestfile 的父文件夹

问题描述 投票:0回答:1

首先我正在过滤最新的文件:

#Copying with variable, get the latest file, descending take  first, timestamp
$latestfile = gci -Attributes !Directory *.xml -Recurse -Force | Sort-Object -Descending -Property LastWriteTime | select -First 1

然后我需要 $latestfile 的上两层父文件夹(我还无法获取它:)

(Get-Item $latestfile).Directory.Parent.Parent.FullName```
It says: path doesnt exist



I am trying to get the second level higher path folder name, then take only the numerical part and add a 1, and then create a folder with that number.
powershell variables directory get parent
1个回答
0
投票

为什么是

-Attributes !Directory
而不是
-File
?另外:

 $LatestFile = Get-ChildItem -File -Force  -Recurse -Filter *.xml |
     Sort-Object -Property LastWriteTime -Descending  | 
     Select-Object -First 1
 $GrandParentDirectory = $LatestFile.Directory.Parent.Parent

此时,您将

$GrandParentDirectory
作为
[DirectoryInfo]
对象,并且可以访问其
.FullName
.Name
属性来根据需要执行操作。

© www.soinside.com 2019 - 2024. All rights reserved.