我有这个脚本:
Get-ChildItem -Path R:\MyFolder\archive -Recurse |
>> Sort-Object -Property LastAccessTime |
>> Select-object -Property Name, FullName
我希望使用不带文件名的Path而不是FullName,因此我尝试进行如下调整:Get-ChildItem -Path R:\MyFolder\archive -Recurse |
>> Sort-Object -Property LastAccessTime |
>> Select-object -Property Name, [System.IO.Path]::GetDirectoryName(fullname)
我显然对PS
非常陌生-是否清楚我正在尝试但失败的事情? 最佳答案
通常,Get-ChildItem
cmdlet返回不同类型的对象:
System.IO.FileInfo
System.IO.DirectoryInfo
不幸的是,在后者上您找不到名为
DirectoryName
的属性。您可以使用Get-ChildItem -File
(-File
参数消除了此类对象),或使用计算所得的属性,如下所示:$rootPath = 'R:\MyFolder\archive'
Get-ChildItem -Path $rootPath -Recurse |
Sort-Object -Property LastAccessTime |
Select-Object -Property Name,
@{ Name = 'DirectoryName';
Expression = { $_.FullName | Split-Path }}
有关说明,请阅读Select-Object => Parameters:关于powershell - 从FullName属性提取路径到结果中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62656575/