Solved

directory and files listing in perl

Posted on 2013-12-22
9
415 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 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
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!

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

717 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