Avatar of weissman
weissman
Flag for Israel asked on

directory and files listing in perl

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
LinuxScripting Languages

Avatar of undefined
Last Comment
weissman

8/22/2022 - Mon
ozo

(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
weissman

ASKER
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.
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
weissman

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
weissman

ASKER
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
ozo

You can customize the date format with POSIX::strftime
weissman

ASKER
Thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.