Solved

directory and files listing in perl

Posted on 2013-12-22
9
408 Views
Last Modified: 2013-12-23
hi All,

i am tring to build a html page that display a directory listing using perl script...

first i have to use perl so i have use:

 my @files = glob("*");

to get all the files and display it in the page  the problem that i need to display also

the file last modify and size!

i have use stat($file)  but it disn'y display the length of files!

here is the code i have write:

my @files = glob("*");

$filesLink="";
foreach $file (@files) {
  @ABC = (stat($file));
  $filesLink = $filesLink."<tr><td valign=\"top\"></td><td><A href=\"maintenance/"."$file"."\">"."$file"."</A></td><td align=\"right\">-----------$ABC['File::stat'][9] ------------------------  </td><td align=\"right\">763K</td><td>&nbsp;</td></tr>";
 
 }

i have also try to use this asample from the internet also it didn't work:

my $path = "maintenance/";

$opendir my $ls,$path or die $!;
while (my $file = readdir $ls ) {
    next if !-f "$path/$file";

    my @stat = stat("$path/$file");
    push @files, [ $file , localtime($stat[9]) ];
}
closedir $ls;

please help
thanks a lot
0
Comment
Question by:weissman
  • 4
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39735541
(stat)[7]  is the length
" $ABC['File::stat'][9] " should give you the warning
Argument "File::stat"  isn't numeric in array element

There should not be a $ in $opendir
0
 

Author Comment

by:weissman
ID: 39735559
hi ozo,

thank you for quick respond ... i have use $ABC['File::stat'][7] but didn't work either

what do you mean by :

Argument "File::stat"  isn't numeric in array element

sorry i am new to perl but i know java

should i use glob or readdir

thanks.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 334 total points
ID: 39735588
perl -W -Mdiagnostics -e "\$ABC['File::stat'][9]"
Argument "File::stat" isn't numeric in array element at -e line 1 (#3)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

Array indices are numbers
'File::stat' is a string.  When you try to use it as a number, it will be treated as a 0
so $ABC['File::stat'] will be evaluated as $ABC[0]
but $ABC['File::stat'][9] doesn't even make sense, since  $ABC[0] is not an array

You may have meant to say
$filesLink .= qq(<tr><td valign="top"></td><td><A href="maintenance/$file">$file</A></td><td align="right">-----------$ABC[7] ------------------------  </td><td align="right">763K</td><td>&nbsp;</td></tr>);
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 total points
ID: 39735593
Here's a simple working demo... though @ozo can tell you if there is anything wrong with it.
#!/usr/bin/perl

    use strict;
    use warnings;

    # my $dir = '/';
		use Cwd;
		my $dir = getcwd;

		print "Content-type:text/html\n\n";
		print "<b>File listing of $dir</b>\n\r<br>";
		print "<table border='0' cellpadding='0' cellspacing='0'>";

    opendir(DIR, $dir) or die $!;

    while (my $file = readdir(DIR)) {

		# Use a regular expression to ignore files beginning with a period
		next if ($file =~ m/^\./);
		next if ($file =~ m/\.bak/);
		my $fsize = -s $file;

		print "<tr><td>$file&nbsp;</td><td>&nbsp;$fsize</td></tr>";

    }
		print "</table>";
    closedir(DIR);
    exit 0;

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:weissman
ID: 39735596
thanks ozo,

although i didn't know what is the difference between $ABC[7] and @ABC[7]  they display the

same result :  (4096)

      test      1386676710      4096

can i ask you how to format the 4096 to be displayed in kb/mb instead

and how to displat last modify in date format ex. 10-Dec-2013 11:58

thanks a lot
0
 
LVL 84

Accepted Solution

by:
ozo earned 334 total points
ID: 39735621
perl -W -Mdiagnostics -e '@ABC[7]'
Scalar value @ABC[7] better written as $ABC[7] at -e line 1 (#1)
    (W syntax) You've used an array slice (indicated by @) to select a
    single element of an array.  Generally it's better to ask for a scalar
    value (indicated by $).  The difference is that $foo[&bar] always
    behaves like a scalar, both when assigning to it and when evaluating its
    argument, while @foo[&bar] behaves like a list when you assign to it,
    and provides a list context to its subscript, which can do weird things
    if you're expecting only one subscript.
   
    On the other hand, if you were actually hoping to treat the array
    element as a list, you need to look into how references work, because
    Perl will not magically convert between scalars and lists for you.  See
    perlref.


print scalar localtime((stat $file)[9]);
0
 

Author Comment

by:weissman
ID: 39735625
thanks a lot to you ozo, Dave

i have managed to display the date in format  using :

$datestring = localtime($ABC[9]);

although it is not my fevorite format but it is okay now

my last question how to format the bytes in MB /KB  i need a function that check the size and if it bigger than 1024 kb then display it in MB instead of kb ...

thanks a lot
0
 
LVL 84

Expert Comment

by:ozo
ID: 39735634
You can customize the date format with POSIX::strftime
0
 

Author Closing Comment

by:weissman
ID: 39735647
Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now