CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Dir Size, No. of Files, and Delete All.

 



benchivers
Novice

Jun 2, 2002, 8:40 AM

Post #1 of 3 (382 views)
Dir Size, No. of Files, and Delete All. Can't Post

I am creating a CGI file manager for the web, and I would appreciate it very much if some would help me with the three following things:



1. I want to able to read the file size of a directory and all the files within it, this directory may include sub-directories with files in those as well. Does anyone know how to do this using CGI.pm, or standard perl code?



2. I would like the program to read the amount of files in total within that directory and within sub-directories to give one big total number.



3. Also, when you use the rmdir function when the directory is not empty, and the directory doesn't delete. Is there anyway you can delete a directory and all the files within that directory and sub-directories in one go??



Any help with this would be most appreciated!

Thanks and many regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


fashimpaur
User / Moderator

Jun 3, 2002, 8:17 AM

Post #2 of 3 (376 views)
Re: [benchivers] Dir Size, No. of Files, and Delete All. [In reply to] Can't Post

Ben,

There is no single fix that handles all of your requests.

I have, however, created several methods here that you may find useful.

I have attached the code and the printout when I used my
"c:/Novell" directory on my computer for the test. I hope
you can use or modify these to fit your requirements.

For quick viewing, this is the code:

Code
  

use
strict;

use Data::Dumper qw( Dumper );


$| = 1;



my $directory = "c:/Novell";

my $size = 0;

$size = sizeOfFilesInDirectory($directory);

my $subDirCount = getSubDirectories($directory);

my @subDirs = getSubDirectories($directory);



print "Total size of all files in $directory: $size\n\n";

print "Number of SubDirectories: $subDirCount\n\n";

print ("SubDirectories:\n\t",join("\n\t",@subDirs),"\n\n") if $subDirCount > 0;

my $dirHash = totalDirectoriesAndFiles($directory);

print "Dumper output:\n\n";

print Dumper($dirHash);


sub sizeOfFilesInDirectory {
my $directory = shift;
my $directorysize = 0;

opendir DIR, $directory;
my @files = grep { !(/^\.+/) && !(-d "$directory/$_") } readdir DIR;
closedir DIR;

foreach my $file(@files){
$directorysize += -s "$directory/$file";
}

return $directorysize;
}

sub getSubDirectories {
my $directory = shift;
opendir DIR, $directory;
my @dirs = grep { !(/^\.+/) && (-d "$directory/$_") } readdir DIR;
closedir DIR;

my $dirCount = @dirs;

return wantarray?@dirs:$dirCount;
}

sub getFiles {
my $directory = shift;
opendir DIR, $directory;
my @files = grep { !(/^\.+/) && !(-d "$directory/$_") } readdir DIR;
closedir DIR;

my $fileCount = @files;

return wantarray?@files:$fileCount;
}

sub totalDirectoriesAndFiles {
my $directory = shift;
my $dircount = 0;
my $filecount = 0;
my $hashout = {};

my @dirs = getSubDirectories($directory);
$dircount = @dirs;
my @files = getFiles($directory);
$filecount = @files;

$hashout->{$directory} = { 'NAME' => $directory,
'SUBDIRS' => [ @dirs ],
'FILES' => [ @files ],
'SUBDIR_COUNT' => $dircount,
'FILE_COUNT' => $filecount,
'BYTES' => sizeOfFilesInDirectory($directory),
};

foreach my $dir(@dirs){
my $returnedHash = totalDirectoriesAndFiles("$directory/$dir");
$hashout = { %$hashout, %$returnedHash };
}

return $hashout;
}


Hope this helps. Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";
Attachments: directorysize.pl (1.90 KB)
  output.txt (16.5 KB)


benchivers
Novice

Jun 4, 2002, 1:41 AM

Post #3 of 3 (370 views)
Re: [fashimpaur] Dir Size, No. of Files, and Delete All. [In reply to] Can't Post

Thanks for your prompt reply, I really appreciate it.

Your methods help me solve the first 2 points on my list, but do you know how to delete all the files and sub-directories within a directory?

Thanks for your work so far.

Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives