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?

Recommended Answers

All 10 Replies

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;

Thanks gerard4143

It work very fine.

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

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"""

This also not work for me?

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

Should you be using \ here

"C:\Users\bt\Desktop\perl_files";

Shouldn't it be

"C:/Users/bt/Desktop/perl_files";

I think you can the rest here

http://perldoc.perl.org/functions/-X.html

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

Since your using warnings and strict you should use my.

my $filesize = -s $filename;

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.