?
Solved

directory and files listing in perl

Posted on 2013-12-22
9
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1336 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 664 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 1336 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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…
Suggested Courses

764 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