gpt4 book ai didi

windows - 如何使用 FileSystemObject.Size 可靠地确定文件夹大小?

转载 作者:可可西里 更新时间:2023-11-01 10:50:27 26 4
gpt4 key购买 nike

为了计算可能非常大的文件夹的大小,我使用了 FileSystemObject.Size :

#! perl

use strict;
use warnings;

use 5.10.0;

use File::Spec;
use Win32::OLE;

my $_fso;
sub folder_size {
my($folder) = @_;

$_fso = Win32::OLE->new("Scripting.FileSystemObject")
unless defined $_fso;

die "GetFolder $folder: $^E"
unless defined(my $f = $_fso->GetFolder($folder));

my $size = $f->Size;
die "Size $folder: $^E" unless defined $size;

$size;
}

对于某些情况,folder_size 正常返回,但对于其他情况,对 Size 的调用会抛出异常。计算 C:\ 中文件夹的大小

my $root = "C:/";
opendir my $dh, $root or die "$0: opendir: $!";

while (defined(my $name = readdir $dh)) {
next if $name eq "." || $name eq "..";
my $folder = File::Spec->catdir($root, $name);
next unless -d $folder;

chomp(my $size = eval { folder_size $folder } // $@);
print "$folder - $size\n";
}

我得到以下输出:

C:\$Recycle.Bin - Size C:\$Recycle.Bin: The resource loader failed to find MUI file at olesize line 22.C:\boot - 17463020C:\cygwin - 1835711453C:\DELL - 133184282C:\doctemp - 12811140C:\Documents and Settings - Size C:\Documents and Settings: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Drivers - 180746384C:\eclipse - 324690795C:\EFI - 262144C:\found.000 - Size C:\found.000: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\ghc - 1014658071C:\gtk2hs - 138050118C:\PerfLogs - Size C:\PerfLogs: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Perl - 115648300C:\Program Files - Size C:\Program Files: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\ProgramData - Size C:\ProgramData: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Python25 - 83902423C:\System Volume Information - Size C:\System Volume Information: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Users - Size C:\Users: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Virtual Machines - 5401825804C:\Windows - Size C:\Windows: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\xampp - 408475320

这似乎至少部分是权限问题,因为以管理员身份运行代码会更改某些输出(以粗体显示)。

C:\$Recycle.Bin - 2062958143C:\boot - 17463020C:\cygwin - 1835711453C:\DELL - 133184282C:\doctemp - 12811140C:\Documents and Settings - Size C:\Documents and Settings: The resource loader failed to find MUI file at olesize line 22.C:\Drivers - 180746384C:\eclipse - 324690795C:\EFI - 262144C:\found.000 - 8950C:\ghc - 1014658071C:\gtk2hs - 138050118C:\PerfLogs - 0C:\Perl - 115648300C:\Program Files - 10857194364C:\ProgramData - Size C:\ProgramData: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Python25 - 83902423C:\System Volume Information - Size C:\System Volume Information: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Users - Size C:\Users: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\Virtual Machines - 5401825804C:\Windows - Size C:\Windows: The resource loader cache doesn't have loaded MUI entry at olesize line 22.C:\xampp - 408475320

MUI 错误信息对应ERROR_MUI_FILE_NOT_FOUND .

我的代码为什么不正确?

对于上下文,我不需要使用 FileSystemObject。我考虑的其他方法是抓取 dir/s 的输出并对子树中所有叶子的大小求和。对于大型目录,dir/s 的输出可能会非常大,搜索整个子树是一种性能狗。 Windows 资源管理器似乎总是能够在给定足够时间的情况下计算出结果,那么有没有办法调用它正在做的事情?

最佳答案

另一种选择是坚持使用纯 perl:Filesys::DiskUsage做同样的事情。但是,我敢打赌许可问题仍然存在。它至少应该比抓取 dir/s 的输出更好。

老实说,如果 Explorer 在您未以管理员身份登录时在这些目录上工作,这对我来说似乎是 Explorer 中的权限升级问题 - 显然,Explorer 可以像管理员一样访问文件系统,即使在它不是这样登录的,所以如果您可以让资源管理器加载您自己的 DLL,您也可以拥有管理员访问权限。

关于windows - 如何使用 FileSystemObject.Size 可靠地确定文件夹大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5764895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com