【问题标题】:Powershell script to sum up files inside a folder. Give me foldername and Filecount总结文件夹内文件的 Powershell 脚本。给我文件夹名称和文件计数
【发布时间】:2013-12-17 10:26:57
【问题描述】:

又是我 :),我还在学习,但会慢慢好起来

这次我在一个脚本上收集文件夹内的文件,但我不知道上面的 2 个文件夹名称。所以我先有一个双通配符。

我需要的是:显示配置文件夹中的文件数,并在之前给我命名这两个文件夹。

完美的应该是这样的:...\folder1\folder2\configurations\ 文件数:3

到目前为止,我的脚本运行良好,但它只是给了我一个很长的列表,如果我在一个文件夹中有 50 个项目,那就太多了。

$sumconfigs = (get-childitem -recurse "C:\appfolder\data\*\*\configurations" | where-object {$_.Name -ne "configuration.zip"} | Where {$_.psIsContainer -eq $false})

foreach ($config in $sumconfigs)
{
write-host $config.FullName
}

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    首先获取您的文件夹列表,然后递归到它们:

    Get-Item 'C:\appfolder\data\*\*\configurations' | % {
      $dir = $_.FullName
      $cnt = Get-ChildItem $dir -Recurse | ? {
               $_.Name -ne 'configuration.zip' -and -not $_.PSIsContainer
             } | Measure-Object | select -Expand Count
      if ($cnt -gt 0) { "{0} number of files: {1}" -f $dir, $cnt }
    }
    

    【讨论】:

    • 谢谢,我可以很好地使用它并对其进行了一些修改。但我无法避免显示结果为 0 的文件夹。 (或至少零非“configuration.zip”。你知道如何实现吗?我试过 count -gt 1 等:/
    • @RayofCommand 只需将输出放在条件中。查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多