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

ADLS Gen1递归统计目录信息 如何通过CLI/PowerShell实现

ADLS Gen1目录遍历及统计实现方案

你可以通过PowerShell调用Azure CLI命令实现递归遍历,也可以选择原生PowerShell模块的方案,无需依赖Azure CLI,两种可直接落地的方案如下:

方案1:PowerShell调用Azure CLI实现递归遍历

该方案完全基于你已知的az dls fs list命令实现,通过递归函数逐层级拉取目录内容,区分文件/目录类型分别统计:

# 基础配置
$dlsAccount = "替换为你的ADLS Gen1账户名"
$rootPath = "/"
$dirStatistics = @{}

# 递归统计函数
function Get-ADLSRecursiveInfo {
    param(
        [string]$CurrentPath
    )
    # 调用AZ CLI拉取当前路径下所有条目,转换为PowerShell对象处理
    $entries = az dls fs list --account $dlsAccount --path $CurrentPath --output json | ConvertFrom-Json
    $currentDirTotalSize = 0
    $currentFormatStats = @{}

    foreach ($entry in $entries) {
        if ($entry.type -eq "DIRECTORY") {
            # 是目录则递归遍历子层
            $subDirResult = Get-ADLSRecursiveInfo -CurrentPath $entry.pathSuffix
            $currentDirTotalSize += $subDirResult.TotalSize
            # 合并子目录的格式统计数据
            foreach ($fmt in $subDirResult.FormatStats.Keys) {
                $currentFormatStats[$fmt] = ($currentFormatStats[$fmt] ? 0) + $subDirResult.FormatStats[$fmt]
            }
        } else {
            # 是文件则累加大小、统计后缀格式
            $currentDirTotalSize += $entry.length
            $fileExt = [System.IO.Path]::GetExtension($entry.name).ToLower()
            $fileExt = if ($fileExt) { $fileExt } else { "无后缀" }
            $currentFormatStats[$fileExt] = ($currentFormatStats[$fileExt] ? 0) + 1
            # 若需统计每种格式的总大小,将上行的1替换为$entry.length即可
        }
    }

    # 保存当前目录的统计结果
    $dirStatistics[$CurrentPath] = [PSCustomObject]@{
        Path = $CurrentPath
        TotalSizeBytes = $currentDirTotalSize
        TotalSizeGB = [math]::Round($currentDirTotalSize / 1GB, 2)
        FileFormatCount = $currentFormatStats
    }

    return [PSCustomObject]@{
        TotalSize = $currentDirTotalSize
        FormatStats = $currentFormatStats
    }
}

# 启动遍历
Get-ADLSRecursiveInfo -CurrentPath $rootPath

# 导出结果为CSV
$dirStatistics.Values | Export-Csv -Path "ADLS全目录统计结果.csv" -Encoding UTF8 -NoTypeInformation

该方案注意事项

  • 首次使用需先执行az login登录Azure账号,执行az account set --subscription "你的订阅ID"切换到对应订阅
  • TB级数据场景下可在az dls fs list后加--page-size 5000参数,减少单次请求负载,避免接口限流

方案2:无需Azure CLI的原生PowerShell方案

使用Azure官方Az.DataLakeStore模块实现,无需依赖Azure CLI环境,对PowerShell的兼容性和执行效率更高:

  1. 先安装依赖模块:
Install-Module -Name Az.DataLakeStore -Scope CurrentUser -Force
  1. 登录Azure并执行遍历统计:
# 登录并切换订阅
Connect-AzAccount
Set-AzContext -SubscriptionId "替换为你的订阅ID"

# 基础配置
$dlsAccount = "替换为你的ADLS Gen1账户名"
$rootPath = "/"
$dirStatistics = @{}

function Get-ADLSNativeRecursiveInfo {
    param(
        [string]$CurrentPath
    )
    $entries = Get-AzDataLakeStoreChildItem -Account $dlsAccount -Path $CurrentPath
    $currentDirTotalSize = 0
    $currentFormatStats = @{}

    foreach ($entry in $entries) {
        if ($entry.Type -eq "Directory") {
            $subDirResult = Get-ADLSNativeRecursiveInfo -CurrentPath $entry.Path
            $currentDirTotalSize += $subDirResult.TotalSize
            foreach ($fmt in $subDirResult.FormatStats.Keys) {
                $currentFormatStats[$fmt] = ($currentFormatStats[$fmt] ? 0) + $subDirResult.FormatStats[$fmt]
            }
        } else {
            $currentDirTotalSize += $entry.Length
            $fileExt = [System.IO.Path]::GetExtension($entry.Name).ToLower()
            $fileExt = if ($fileExt) { $fileExt } else { "无后缀" }
            $currentFormatStats[$fileExt] = ($currentFormatStats[$fileExt] ? 0) + 1
        }
    }

    $dirStatistics[$CurrentPath] = [PSCustomObject]@{
        Path = $CurrentPath
        TotalSizeGB = [math]::Round($currentDirTotalSize / 1GB, 2)
        FileFormatCount = $currentFormatStats
    }

    return [PSCustomObject]@{
        TotalSize = $currentDirTotalSize
        FormatStats = $currentFormatStats
    }
}

Get-ADLSNativeRecursiveInfo -CurrentPath $rootPath
$dirStatistics.Values | Export-Csv -Path "ADLS全目录统计结果.csv" -Encoding UTF8 -NoTypeInformation

TB级数据场景优化建议

  • 按顶层目录拆分遍历任务,分批次执行,避免单线程长时间运行触发Azure接口限流
  • 关闭不必要的中间日志打印逻辑,仅保留最终结果导出步骤,可提升30%以上的执行效率
  • 若目录层级超过100级,将递归实现改为栈结构的非递归实现,避免触发PowerShell默认的递归深度限制报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 03:45:04