请考虑以下目录树:
root
dir1
dir11
x.L01 12kb
x.L02 10kb
dir12
dir122
a.jpg 5kb
b.xls 3kb
c.bmp 3kb
dir2
a.L01 100kb
a.L02 200kb
a.L03 50kb
dir3
dir31
dir4有3种可能的情况:
root/dir3/dir31和root/dir4L0x文件,其中x是一个数字;root/dir1/dir11和root/dir2L0x-kind的文件。所需的输出是一个包含3列的自定义目录:
其逻辑如下:
L0x文件,则只列出第一个文件(root/dir1/dir11/x.L01),但要计算所有L01的文件数和文件总数。因此,示例输出将是:
path size count
----------------------------------------
root/dir1/dir11/x.L01 22kb 2
root/dir1/dir12/dir122 11kb 3
root/dir2/a.L01 350kb 3我刚刚开始使用powershell,我想出了以下几点,这不是很多,但是(a)我的方向正确吗?以及(b)如何从这里着手?
Get-ChildItem "C:\root" -Recurse |
Foreach-Object {
If ($_.PSIsContainer) {
Get-ChildItem $_.fullname |
Foreach-Object {
Write-Host $_.fullname
}
}
}任何帮助都将不胜感激!
发布于 2015-07-16 13:44:21
这可以随着你的需求的变化而变化。这将作为一个自定义对象创建所需的输出,您可以根据需要操作和导出该对象。
$rootPath = "c:\temp"
Get-ChildItem $rootPath -Recurse |
Where-Object {$_.PSIsContainer} |
Where-Object {(Get-ChildItem $_.FullName | Where-Object {!$_.PSIsContainer}| Measure-Object | Select-Object -ExpandProperty Count) -gt 0} |
ForEach-Object{
$files = Get-ChildItem $_.FullName
$props = @{
Path = $_.FullName
Size = "{0:N0} KB" -f (($files | Where-Object {!$_.PSIsContainer} | Measure-Object -Sum Length | Select-Object -ExpandProperty Sum) / 1024)
Count = $files | Measure-Object | Select-Object -ExpandProperty Count
}
If($files.Extension -match "L\d\d"){
# These are special files and we are assuming they are alone in the directory
# Change the path
$props.Path = $files | Where-Object {!$_.PSIsContainer} | Select-Object -First 1 | Select-Object -ExpandProperty FullName
}
New-Object -TypeName PSCustomObject -Property $props
} | Select Path,Size,Count递归获取$rootPath的所有文件夹和文件。根据所有文件和空文件夹的直接内容过滤掉它们。然后构建一个包含所有请求详细信息的自定义对象。如果结果是存在L0X文件,那么使用找到的第一个路径来更新路径。
目前,我假设所有文件都是L0X格式的。如果需要的话我们可以确认。
样本输出
Path Size Count
---- ---- -----
C:\temp\win64 1,092 KB 2
C:\temp\Empy\Stuff\New Text Document - Copy.L01 0 KB 2https://stackoverflow.com/questions/31453748
复制相似问题