Get-FolderItem版本获取所有文件夹

时间:2015-09-06 12:22:44

标签: powershell robocopy

我找到了一个cmdlet,无论路径大小如何,都会返回所有文件。我想知道是否有一个等效的命令,无论路径大小如何都能获得所有文件夹?

Get-FolderItem结合了robocopy和powershell,即使路径大于260的文件也会返回。

无论如何都要获取文件共享中的所有文件夹

1 个答案:

答案 0 :(得分:1)

Get-FolderItem使用robocopy开关/NDL从日志输出中排除目录,用于获取文件信息。它还可以使用/S开关来递减而不是/E

来避免空文件夹

只需更改$params变量:

$params.AddRange(@("/L","/S","/NJH","/BYTES","/FP","/NC","/NDL","/TS","/XJ","/R:0","/W:0"))

$params.AddRange(@("/L","/E","/NJH","/BYTES","/FP","/NC","/NFL","/TS","/XJ","/R:0","/W:0"))

现在,Robocopy将列出目录,而不是文件。由于目录的输出与文件略有不同,因此您还必须稍微更改解析逻辑。

更改

If ($_.Trim() -match "^(?<Size>\d+)\s(?<Date>\S+\s\S+)\s+(?<FullName>.*)") {
    $object = New-Object PSObject -Property @{
        ParentFolder = $matches.fullname -replace '(.*\\).*','$1'
        FullName = $matches.FullName
        Name = $matches.fullname -replace '.*\\(.*)','$1'
        Length = [int64]$matches.Size
        LastWriteTime = [datetime]$matches.Date
        Extension = $matches.fullname -replace '.*\.(.*)','$1'
        FullPathLength = [int] $matches.FullName.Length
    }
    $object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
    Write-Output $object
}

If ($_.Trim() -match "^(?<Children>\d+)\s+(?<FullName>.*)") {
    $object = New-Object PSObject -Property @{
        ParentFolder = $matches.fullname -replace '(.*\\).*','$1'
        FullName = $matches.FullName
        Name = $matches.fullname -replace '.*\\(.*)','$1'
    }
    $object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
    Write-Output $object
}

那应该这样做