FLEX: getting a folder size

2019-07-24 06:10发布

问题:

I'm triying to get a folder size by doing:

var FolderFile:File = new File("file:///SomePath/Folder");
var FolderSize: FolderFile.size;

But this gives me a value of 0, how can I get the folder size? is there anyway to do this?

Tranks

回答1:

No, there's no way to do it automagically. Getting the size of the directory is a complex and potentially painfully slow operation. There could be 10s of thousands of files in a directory, or a directory could be located on a (slow?) network, not to mention tape storage and similar scenarios.

The file systems themselves don't store directory size information, and the only way to know it is to calculate it file-by-file, there's no quick/easy shortcut. So, you will have to rely on the solution you posted above, and, yes, it is going to be slow.



回答2:

I want to know the size of the folder (like 10mb). Sorry for the second line, I write it wrong, it's:

 var Foldersize:Number = FolderFile.size;

I just made a new class wich executes this function:

        public function GetFolderSize(Source:Array):Number
    {
        var TotalSizeInteger:Number = new Number();
        for(var i:int = 0;i<Source.length;i++){
            if(Source[i].isDirectory){
                TotalSizeInteger +=   this.GetFoldersize(Source[i].getDirectoryListing());
            }
            else{
                TotalSizeInteger += Source[i].size;
            }
        }
        return TotalSizeInteger;
    }

In "Source" you pass the FolderFile.getDirectoryListing(), something like this:

 var CC:CustomClass = new CustomClass();
 var FolderSize:Number = CustomClass.GetFolderSize(FolderFile.getDirectoryListing());

But this is a very slow method, is there a more quick and easy way to know the folder size?

Sorry for my grammar, i'm just learning english.

Thanks