Skip to content

Instantly share code, notes, and snippets.

@axiixc
Created December 28, 2010 03:15
Show Gist options
  • Save axiixc/756858 to your computer and use it in GitHub Desktop.
Save axiixc/756858 to your computer and use it in GitHub Desktop.
Quickly tells some info about a directory of text files
#!/usr/bin/php
<?php # Axiixc [ 2010 ]
function exByteSize($bytes)
{
$size = $bytes / 1024;
if ($size < 1024)
{
$size = number_format($size, 2);
$size .= ' KB';
}
else
{
if ($size / 1024 < 1024)
{
$size = number_format($size / 1024, 2);
$size .= ' MB';
}
else if ($size / 1024 / 1024 < 1024)
{
$size = number_format($size / 1024 / 1024, 2);
$size .= ' GB';
}
else if ($size / 1024 / 1024 / 1024 < 1024)
{
$size = number_format($size / 1024 / 1024 / 1024, 2);
$size .= ' TB';
}
}
return $size;
}
$input = $argv[1];
$files = array();
if (is_dir($input))
{
$files = scandir($input);
foreach ($files as $key => $value)
$files[$key] = $input . '/' . $value;
}
else
$files[] = $input;
$numLines = 0;
$numFiles = 0;
$size = 0;
foreach ($files as $file)
{
if (substr($file, -9) != '.DS_Store' && !is_dir($file))
{
$numLines += count(file($file));
$numFiles++;
$size = filesize($file);
}
}
printf("Files %d\nLines %d\nSize %s\n", $numFiles, $numLines, exByteSize($size));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment