You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Get-AzDataLakeGen2ChildItem仅递归前两层文件夹?

实现仅递归遍历Azure Data Lake Gen2前两层文件夹的方法

Get-AzDataLakeGen2ChildItem没有直接限制递归层数的参数,要实现仅遍历前两层,得手动分层处理:先获取根目录的第一层内容,再遍历第一层里的文件夹,获取它们的直接子项(第二层),具体代码如下:

处理分页的完整示例

$maxReturn = 1000
$token = $null
$firstLevelItems = @()

# 1. 获取根目录下的第一层所有项(包含文件夹和文件),处理分页
do {
    $batch = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem $Container.Name -MaxCount $maxReturn -ContinuationToken $token
    $firstLevelItems += $batch
    $token = $batch[$batch.Count - 1].ContinuationToken
} while ($null -ne $token)

# 2. 存储前两层的所有项,先加入第一层内容
$allItems = $firstLevelItems.Clone()

# 3. 遍历第一层的每个文件夹,获取其直接子项(第二层),处理分页
foreach ($folder in $firstLevelItems | Where-Object { $_.GetType().Name -eq 'FileSystemDirectory' }) {
    $token = $null
    do {
        $batch = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem $Container.Name -Path $folder.Name -MaxCount $maxReturn -ContinuationToken $token
        $allItems += $batch
        $token = $batch[$batch.Count - 1].ContinuationToken
    } while ($null -ne $token)
}

# 4. 输出所有前两层的项
foreach ($item in $allItems) {
    Write-Host "Item: $($Container.Name)/$($item.Path)"
}

关键说明

  • 第一层:直接调用不带-Path和-Recurse的命令,获取容器根目录下的所有内容
  • 第二层:只针对第一层里的文件夹,调用命令时指定-Path为文件夹名称(不带-Recurse),获取该文件夹下的直接子项,不会递归更深层级
  • 分页处理:保留了你原代码中的-MaxCount和ContinuationToken逻辑,确保项数较多时能完整获取

内容的提问来源于stack exchange,提问作者hein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 21:41:04