This ASP function counts files of a specified extension inside a folder: this is done by creating a file system object, open folder and scan files for matching the extension. The match of the extension is made without regular expression but using string functions: Right, Len and Ucase.
Function countFiles(path,ext)
Dim fs, folder, c
c = 0
Set fs = CreateObject("Scripting.FileSystemObject")
Set folder = fs.GetFolder(path)
For Each item In folder.Files
If UCase(Right(item.Name, Len (ext))) = UCase(ext)) Then c=c+1
Next
countFiles = c
End Function
Tags: dir, file, filesystemobject, getfolder
This small PHP function lets you calculate the dir size entering each sub dir and making the sum of the filesize of every file contained. Returns an array of two values: size and numbers of file. The second function shows how format the size in a more readable way (with abbreviation MB, KB, GB).
function dirsize($dir) {
if(is_file($dir)) return array('size'=>filesize($dir),'howmany'=>0);
if($dh=opendir($dir)) {
$size=0;
$n = 0;
while(($file=readdir($dh))!==false) {
if($file=='.' || $file=='..') continue;
$n++;
$data = $this->dirsize($dir.'/'.$file);
$size += $data['size'];
$n += $data['howmany'];
}
closedir($dh);
return array('size'=>$size,'howmany'=>$n);
}
return array('size'=>0,'howmany'=>0);
}
If you want to show the file size in a more readable way you can use this second function to format the value:
function file_size($fsizebyte) {
if ($fsizebyte < 1024) {
$fsize = $fsizebyte." bytes";
}elseif (($fsizebyte >= 1024) && ($fsizebyte < 1048576)) {
$fsize = round(($fsizebyte/1024), 2);
$fsize = $fsize." KB";
}elseif (($fsizebyte >= 1048576) && ($fsizebyte < 1073741824)) {
$fsize = round(($fsizebyte/1048576), 2);
$fsize = $fsize." MB";
}elseif ($fsizebyte >= 1073741824) {
$fsize = round(($fsizebyte/1073741824), 2);
$fsize = $fsize." GB";
};
return $fsize;
}
Tags: count, file, filesize, gigabyte, megabyte, opendir, recursive, recursively