基于文件/文件夹名称的文件夹大小总和 [英] Sum of file folder size based on file/folder name

查看:48
本文介绍了基于文件/文件夹名称的文件夹大小总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个包含数百/数千个数据库的 SQL Server 中拥有多个文件夹.每个数据库包含三个元素:

I have multiple folders across a number of SQL Servers that contain hundreds/thousands of databases. Each database comprises of three elements:

<dbname>.MDF
<dbname>.LDF
<dbname>files (Folder that contains db files/attachments)

我需要将这些文件合并在一起并加起来它们的总大小,有人对如何做到这一点有任何建议吗?

I need to marry these files together and add up their total size, does anyone have any advice on how to do this?

只是为了澄清,我目前能够输出 MDF/LDF 文件的文件大小,我有一个单独的脚本来总结文件夹大小.我需要一种在名称匹配时将 .MDF/.LDF/DBFiles 文件夹添加在一起的方法.请记住,所有文件都以数据库名称为前缀.

EDIT : Just to clarify, I'm currently able to output the filesizes of the MDF/LDF files, I have a separate script that summarises the folder sizes. I need a method of adding together a .MDF/.LDF/DBFiles folder when their name matches. Bearing in mind all of the files are prefixed with the database name.

编辑 #2:到目前为止给出的 2 个选项将 .mdf/.ldf 文件相加没有问题,但不添加 DBFiles 文件夹的文件夹大小.有没有人对如何修改这些脚本以包含以相同名称开头的文件夹有任何意见.

EDIT #2: The 2 options given so far sum together the .mdf/.ldf files with no problem, but do not add the folder size of the DBFiles folder. Does anyone have any input on how to amend these scripts to include a folder beginning with the same name.

首先提供的脚本:

$root = 'C:\db\folder'
Get-ChildItem "$root\*.mdf" | Select-Object -Expand BaseName |
ForEach-Object {
New-Object -Type PSObject -Property @{
  Database = $_
  Size     = Get-ChildItem "$root\$_*" -Recurse |
             Measure-Object Length -Sum |
             Select-Object -Expand Sum
}
}

提供的第二个脚本:

gci "c:\temp" -file -Include "*.mdf", "*.ldf" -Recurse | 
group BaseName, DirectoryName  | 
%{new-object psobject -Property @{FilesAndPath=$_.Name; Size=($_.Group | gci |  Measure-Object Length -Sum).Sum }    }

编辑 #3:

感谢 Ansgar(见下文),更新后的解决方案完美地解决了这个问题.使用最终解决方案更新问题:

Thanks to Ansgar (below), the updated solution has done the trick perfectly. Updating question with final solution:

$root = 'C:\db\folder'
Get-ChildItem "$root\*.mdf" | Select-Object -Expand BaseName |
  ForEach-Object {
    New-Object -Type PSObject -Property @{
      Database = $_
      Size     = Get-ChildItem "$root\$_*\*" -Recurse |
                 Measure-Object Length -Sum |
                 Select-Object -Expand Sum
    }
  }

推荐答案

仅枚举数据库文件夹中的 .mdf 文件,然后枚举每个基本名称的文件和文件夹.

Enumerate just the .mdf files from your database folder, then enumerate the files and folders for each basename.

$root = 'C:\db\folder'
Get-ChildItem "$root\*.mdf" | Select-Object -Expand BaseName |
  ForEach-Object {
    New-Object -Type PSObject -Property @{
      Database = $_
      Size     = Get-ChildItem "$root\$_*\*" -Recurse |
                 Measure-Object Length -Sum |
                 Select-Object -Expand Sum
    }
  }

这篇关于基于文件/文件夹名称的文件夹大小总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆