使用Powershell查找磁盘上的目录空间



我想知道我不同子目录的大小,所以我遵循了获取目录大小powershell和在powershell中的显示目录结构以及SO和其他地方的其他一些指示。这些功能运行良好,除了我想知道一些部分同步的文件夹在磁盘中占用的空间(例如,我在 Skydrive 中有一个文件夹,其中只有一些子文件夹在线可用(。当我调用任一函数时,它们会报告文件夹的大小,就好像它在磁盘中一样,而不是实际的磁盘使用情况。如果我右键单击该文件夹并检查其属性,我可以清楚地看到sizesize in disk之间存在差异。

我已经尝试过将Sort-Object -Property SizeSort-Object -Property Length切换到任一函数(主要基于 Get-ChildItem 函数(,但它们都返回磁盘大小而不是大小。

我想有一个简单的开关可以使其仅考虑实际磁盘使用情况,而不是总大小,但我完全不知道如何实现它。任何线索都非常感谢!

谢谢

磁盘大小是文件在驱动器上占用的实际大小,具体取决于簇大小(或分配单元(,大多数情况下为 4KB,但不是全部。这取决于文件格式及其格式设置方式。

只要文件没有被压缩,就要找出需要多少个集群块来容纳每个文件。请记住,如果文件小于簇大小,它将占用一个分配单元。

如果文件被压缩,则信息不容易获得,需要通过 API 检索。

以下代码分为 3 个主要部分:

  1. 它定义了一个用于访问内核.dll中的函数GetCompressedFileSizeAPI的类型。此函数将检索文件磁盘上的压缩大小。
  2. 它使用 WMI 来确定给定$path的群集大小
  3. 它根据文件是否压缩来计算$path的文件夹和子文件夹中文件的磁盘大小。

请注意,对Get-ChildItem的调用使用 -force 开关来确保我们也检索隐藏文件和系统文件。

我不确定此代码是否适用于Skydrive,因此您可能需要更改WMI部分。

$path = '.'
Add-Type -TypeDefinition @" 
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
public class FileInfo 
{ 
    [DllImport("kernel32.dll", SetLastError=true, EntryPoint="GetCompressedFileSize")]
    static extern uint GetCompressedFileSizeAPI(string lpFileName, out uint lpFileSizeHigh);
    public static ulong GetCompressedFileSize(string strFileName)
    {
        uint intHigh;
        uint intLow;
        intLow = GetCompressedFileSizeAPI(strFileName, out intHigh);
        int intError = Marshal.GetLastWin32Error();
        if (intHigh == 0 && intLow == 0xFFFFFFFF && intError != 0)
            throw new Win32Exception(intError);
        else
            return ((ulong)intHigh << 32) + intLow;
    }
} 
"@

$files = Get-ChildItem $path -Recurse -force | where {$_.PSIsContainer -eq $false}
$drive = [string]$files[0].PSdrive+':'
$wql = "SELECT Blocksize FROM Win32_Volume where DriveLetter='$drive'"
$driveinfo = Get-WmiObject -Query $wql -ComputerName '.' 
$sizeondisk = ($files | %{      
    if ($_.Attributes -like "*compressed*")
    {
        if ($_.length -lt $driveinfo.BlockSize -and $_.length -ne 0)
        {
            $driveinfo.BlockSize
        }
        else
        {
            [FileInfo]::GetCompressedFileSize($_.fullname)
        }
    }
    else
    {
        if ($_.length -lt $driveinfo.BlockSize -and $_.length -ne 0)
        {
            $driveinfo.BlockSize
        }
        else
        {
            ([math]::ceiling($_.length/$driveinfo.BlockSize))*$driveinfo.BlockSize
        }
    }
}|Measure -sum).sum
$sizeondisk

稀疏文件的更新:

让我们看看这个版本是否适用于稀疏文件,在前面代码的末尾添加此代码块,保持其他所有内容相同:

$sparsesize = ($files | %{      
    if ($_.length -lt $driveinfo.BlockSize -and $_.length -ne 0)
    {
        $driveinfo.BlockSize
    }
    else
    {
        $_.fullname
        [FileInfo]::GetCompressedFileSize($_.fullname)
    }
}|Measure -sum).sum
$sparsesize

来源:

了解 NTFS 压缩

帮助查询文件以在磁盘上返回大小

压缩文件大小(法语(

如何从PowerShell获取文件的实际磁盘大小?

NTFS、FAT 和 exFAT 的默认簇大小

出于某种原因,我在稀疏文件代码片段中遇到了有关某些值是字符串而不是数字的错误。

但是,当我从下面取出" $_.fullname"时,我注意到

$sparsesize = ($files | %{      
if ($_.length -lt $driveinfo.BlockSize -and $_.length -ne 0)
{
    $driveinfo.BlockSize
}
else
{
    $_.fullname
    [FileInfo]::GetCompressedFileSize($_.fullname)
}
}|Measure -sum).sum
$sparsesize

并将其更改为

$sparsesize = ($files | %{      
if ($_.length -lt $driveinfo.BlockSize -and $_.length -ne 0)
{
    $driveinfo.BlockSize
}
else
{
    [FileInfo]::GetCompressedFileSize($_.fullname)
}
}|Measure -sum).sum
$sparsesize

然后它返回一个数值。

最新更新