尝试计算可能非常大的文件夹的大小,我使用了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 - 17463020 C:\cygwin - 1835711453 C:\DELL - 133184282 C:\doctemp - 12811140 C:\Documents and Settings - Size C:\Documents and Settings: The resource loader cache doesn't have loaded MUI entry at olesize line 22. C:\Drivers - 180746384 C:\eclipse - 324690795 C:\EFI - 262144 C:\found.000 - Size C:\found.000: The resource loader cache doesn't have loaded MUI entry at olesize line 22. C:\ghc - 1014658071 C:\gtk2hs - 138050118 C:\PerfLogs - Size C:\PerfLogs: The resource loader cache doesn't have loaded MUI entry at olesize line 22. C:\Perl - 115648300 C:\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 - 83902423 C:\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 - 5401825804 C:\Windows - Size C:\Windows: The resource loader cache doesn't have loaded MUI entry at olesize line 22. C:\xampp - 408475320
它似乎至少部分是权限问题,因为以管理员身份运行代码会更改某些输出(以粗体显示)。
C:\$Recycle.Bin - 2062958143 C:\boot - 17463020 C:\cygwin - 1835711453 C:\DELL - 133184282 C:\doctemp - 12811140 C:\Documents and Settings - Size C:\Documents and Settings: The resource loader failed to find MUI file at olesize line 22. C:\Drivers - 180746384 C:\eclipse - 324690795 C:\EFI - 262144 C:\found.000 - 8950 C:\ghc - 1014658071 C:\gtk2hs - 138050118 C:\PerfLogs - 0 C:\Perl - 115648300 C:\Program Files - 10857194364 C:\ProgramData - Size C:\ProgramData: The resource loader cache doesn't have loaded MUI entry at olesize line 22. C:\Python25 - 83902423 C:\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 - 5401825804 C:\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 Explorer总是能够在给定足够时间的情况下计算结果,所以有没有办法调用它正在做的任何事情?
答案 0 :(得分:0)
另一个选择是坚持使用pure-perl:Filesys::DiskUsage做同样的事情。但是,我打赌许可问题仍将存在。它应该至少比刮取dir /s
的输出更好。
老实说,如果当你没有以管理员身份登录时,如果资源管理器在这些目录上工作,这似乎是资源管理器中的权限升级问题 - 显然,资源管理器可以像管理员一样访问文件系统,即使它没有这样登录,所以如果你可以通过资源管理器加载你自己的DLL,你也可以拥有管理员权限。