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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Perl Directory Tree Program


New User

Nov 29, 2013, 12:47 PM

Post #1 of 4 (13201 views)
Perl Directory Tree Program Can't Post

Hello everyone, I'm new to writing perl scripts and I need help with a program assignment.

The assignment is to Develop a program to analyze the directory structure of a Linux disk and identify any files larger that 500kbytes. But I need to use a for each loop and sub routine within the program.

I have only gotten up to this but, I'm not sure if its the right direction or not.

use warnings;

my $directory = '/home';

opendir (DIRFH, $directory) or die "Can't open directory! $! \n";

my @direct = $directory;

for each
$directory (@direct)
if (-d $file) {
print "This is a directory: " . $file;

(This post was edited by perlbegginer02 on Nov 29, 2013, 12:49 PM)


Nov 30, 2013, 8:04 AM

Post #2 of 4 (13159 views)
Re: [perlbegginer02] Perl Directory Tree Program [In reply to] Can't Post

There are many ways to do this assignment. You have a start on one of them. Add 'use strict;' and fix all the problems that perl tells you about. Then ask for help again.
Good Luck,


Dec 1, 2013, 2:12 AM

Post #3 of 4 (13116 views)
Re: [perlbegginer02] Perl Directory Tree Program [In reply to] Can't Post

An approach which is out of scope of your assignment, but nonetheless shows a proper way to perform this task, which you can study later:

use strict;
use warnings;
use Cwd;
use File::Find;
use File::stat;
use Data::Dumper;

my $min_kb = 500;
my @output;
find( sub { push @output, $File::Find::name if (stat($File::Find::name)->size / 1024 >= $min_kb) }, getcwd );
print Dumper \@output;


(This post was edited by Zhris on Dec 1, 2013, 2:17 AM)


Dec 1, 2013, 2:24 AM

Post #4 of 4 (13110 views)
Re: [perlbegginer02] Perl Directory Tree Program [In reply to] Can't Post

With regards to your assignment, although syntaxly incorrect, you are correctly using a foreach loop to attempt to transverse through each file of the directory. For the subroutine, you will likely need one for recursion IF you need to process child directories. You would also probably use a subroutine which takes a filepath, calculates the filesize in kb, and possibly push the filepath onto your output array if it exceeds 500.



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

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