我有一个包含子目录的测试目录:
C:\test
- test1
- test-1-1
- DIR1
- 1.0
- test-1-2
- DIR2 => Latest modified sub dir
- 1.1
- test2
- test-2-1
- DIR1 => Latest modified sub dir
- 1.3
- test-2-2
- DIR1
- 1.2可以为测试中的每个子目录输出最近修改过的子目录的名称和版本。假设DIR1或DIR2可以是任何名称。
最终视图更类似于:
test-1-2 1.1
test-2-1 1.3到目前为止,我能得到的输出只有最新的test*-*-*,没有DIR*
gci 'C:\test' |where { $_.psiscontainer } |foreach { get-childitem $_.name |sort creationtime | select -expand name -last 1 }
PS C:\test> gci 'C:\test' |where { $_.psiscontainer } |foreach { get-childitem $_.name |sort creationtime | select -expand name -last 1 }
test-1-2
test-2-1发布于 2019-03-26 00:32:22
我不太明白你的问题。你能试着重新表述这个问题吗,或者给出更多/不同的例子?
我猜测了一下你想要实现的目标。看一下下面的代码。
我假设你的文件夹结构总是一样的。输出将向您显示每个test--文件夹中名为"DIR“的最新文件夹。我把文件夹的深度组织成了“层次”,希望能让事情更清晰一些。
我经常发现,与在管道中使用' ForEach -Object‘相比,使用ForEach和s脚本块可以让您对数据进行更多的控制。
$Level1 = Get-ChildItem -Path "C:\test" -Directory
ForEach ($D1 in $Level1)
{
Write-Host "Getting ChildItem of DIR: $($D1.FullName)"
$Level2 = Get-ChildItem -Path $D1.FullName -Directory
ForEach ($D2 in $Level2)
{
Write-Host "Getting ChildItem of DIR: $($D2.FullName)`n`n"
$LastModDir = Get-ChildItem -Path $D2.FullName -Directory | Sort-Object CreationTime | Select-Object Name -Last 1
Write-Host "$D2 $($LastModDir.Name)"
}
}输出:
test-1-1 DIR1
test-1-2 DIR2
test-2-1 DIR1
test-2-2 DIR2即使这不是你试图实现的目标,这些技术也可能对你实现目标有用。
编辑:我只是注意到你想要排序的是1.1etc文件夹,而不是DIR文件夹。只需使用ForEach循环添加另一个“级别”即可。
https://stackoverflow.com/questions/55340755
复制相似问题