Solved

directory and files listing in perl

Posted on 2013-12-22
9
413 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 83

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
 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

831 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