On Thu, 09 Jul 2009, Ramkumar wrote:
> In windows explorer, files in a folder displaying (name sort) as,
> Chapter 1.doc
> Chapter 2.doc
> Chapter 3.doc
> Chapter 4.doc
> Chapter 12.doc
> Chapter 20.doc
> Chapter 21.doc
> Chapter 30.doc
> Chapter 31.doc
> Chapter 40.doc
> Chapter 41.doc
> Chapter 42.doc
>
> I need the above list/result using Perl. But Perl gives result as below,
Explorer uses the StrCmpLogicalW() function in shlwapi.dll to sort filenames
into "logical" order. The exact sort order is not guaranteed and may change
with each Windows release or service pack! You can call it from Perl if you
are using Windows XP or later. See sample code below.
Cheers,
-Jan
#!perl
use strict;
use warnings;
use Encode qw(encode);
use Win32::API;
my $cmp_logical = Win32::API->new("shlwapi.dll", "StrCmpLogicalW", "PP", "I");
die "Needs shlwapi.dll version 5.5 or later\n" unless defined $cmp_logical;
sub cmp_logical {
return $cmp_logical->Call(encode("UTF-16LE", $a), encode("UTF-16LE", $b));
}
my @names = <DATA>;
print for sort cmp_logical @names;
__DATA__
Chapter 1.doc
Chapter 12.doc
Chapter 2.doc
Chapter 20.doc
Chapter 21.doc
Chapter 3.doc
Chapter 30.doc
Chapter 31.doc
Chapter 4.doc
Chapter 40.doc
Chapter 41.doc
Chapter 42.doc
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs