Hi
I have 4 files in directory on my desktop?
My question is

1.How can I display for each file only the name, the size and the date in this order?
2. How can I display all files sorted accorded their name?

#!/usr/bin/perl
use strict;
use warnings;

my $dir = 'C:\Users\bt\Desktop\perl_files';

opendir(DIR, $dir) or die $!;
my @files = sort { $a <=> $b } readdir(DIR);
while (my $file = readdir(DIR)) {

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

    print "$file\n";

}

closedir(DIR);
exit 0;

But i get error?

Edited 3 Years Ago by tony75

Try this

my @files = sort { $a cmp $b } readdir(DIR);

I tried this and it worked.

#!/usr/bin/perl

use strict;
use warnings;

my $dir = 'directory_path';

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

foreach( sort { $a cmp $b } readdir(DIR))
{
        next if (/^\./);
        print $_,"\n";
}

closedir(DIR);
exit 0;

Edited 3 Years Ago by gerard4143

Thanks gerard4143

It work very fine.

How can I display for each file only the name, the size and the date in this order?

Edited 3 Years Ago by tony75

Thanks again
Infact I already tried with this link before I post but its not work for me,Idont know why!

I mean creation data.
and its good to know las modified date.

use strict;
use warnings;
my $filesize = -s "C:\Users\bt\Desktop\perl_files";
print "Size: $filesize\n";

exit 0;

"""Unrecognized escape \D passed through at line 4 """

"""Unrecognized escape \p passed through at"""
"""Use of uninitialized value $filesize in concatenation (.) or string at 



line 6"""

Edited 3 Years Ago by tony75

This also not work for me?

my $filename = "C:\Users\nt\Desktop\perl_files";
$filesize = -s $filename;
print $filesize; 

Thanks Sir
As you see I'm using "C:\Users\bt\Desktop\perl_files";

And I cann't see somthing wron with this code!

    my $filename = "C:\Users\bt\Desktop\perl_files";
    $filesize = -s $filename;
    print $filesize; 

I get this error

Global symbol "$filesize" requires explicit package name at

Edited 3 Years Ago by tony75

Hi tony75,

There are more than one way to achieve your aim. Of course, using the function opendir and reading all your filename like you did should work, but why not use one of the CORE module to go through the file for you save the filename and other requirement into a hash, then you sort your data as deem fit.
Please see below script:

#!/usr/bin/perl
use warnings;
use strict;
use File::Find qw(find);

my $directory = $ARGV[0] // '.';

my %file_info;        ## hash for file information
my $name_size = 0;    ## get filename size

find( \&show_stat, $directory );    ## module in use

sub show_stat {

    return if /^\.{1,}/;            ## return for . or ..

    ## get the length of the longest filename
    $name_size = length($_) > $name_size ? length($_) : $name_size;

    ## populate the hash with filename as key, size and date as value
    push @{ $file_info{$_} }, -s ($_), scalar localtime( ( stat($_) )[9] );
}

## print out your required values
print join "  " =>
  map { $_, q{ } x ( $name_size - length($_) ), @{ $file_info{$_} }, $/ }
  sort { $a cmp $b } keys %file_info;

Please note, you are to specify your directory from the CLI -- Command Line Interface, as I don't like hardcoring directory into code, because there might be reasons to change that anytime soon. And as this script goes even if you don't specify any directory to check, it check the directory from where the script is been run by default. However, you can change this script to work for you as you like.

Hope this helps

Edited 3 Years Ago by 2teez

Thanks 2teez it work very fine
And thanks for you help gerard4143.

This question has already been answered. Start a new discussion instead.