Can I sort files according to the length of filename?

  • you should provide your platform ? Jan 7, 2011 at 3:06
  • 2
    @nXqd see tags.
    – John T
    Jan 7, 2011 at 3:08

5 Answers 5


Since you haven't specified a preferred method, here is a solution in Perl:

use strict;
use warnings;

my @a;

opendir(my $dir, ".") or die $!;
while(readdir $dir) {
    push @a, $_;
closedir $dir;

@a = sort { length($a) <=> length($b) } @a;

    print "$_\n" if -f;


  • You can use readdir() in a list context - the while loop is unnecessary. (Spaghetti Perl mode: $\ = "\n"; print if -f for sort {length($a) <=> length($b)} readdir $dir;)
    – user1686
    Jan 7, 2011 at 6:14

Output the contents of a folder, sorted by file name length using PowerShell:

 gci c:\anyfolder | select-object name, @{Name="Nlength";Expression={$_.Name.Length}} | sort-object Nlength

It'll output something like this:

Name                     Nlength
----                     -------
DL.mdb                         6
trolol.txt                    10
AAAAA-2011-03-23-111.xls      24
  • How does this work? I can't really comprehend what you're doing here May 16, 2020 at 11:36

Using PowerShell:

# example 1
Get-ChildItem | Sort-Object { $_.Name.Length }
# example 2
ls | sort { $_.Name.Length }



I realize I am a bit late to the party, but yes you can, in Windows at least! See FreeCommander (https://freecommander.com/en/summary/), a dual-pane file browser / manager with so many great features I can't even list them all. Do you miss the clarity and speed of Windows XP file search? Try CTRL-F in any directory using FreeCommander. Losing your marbles drilling down into overly-complex file structures over and over again? Try creating favourites shortcuts to frequently used directories (local and network). Working with too many directories at once? Both panes are fully tabbed to allow quick switching. Compare directory contents with a click? Check. Free for both personal and commercial use? Check. Did I mention there's a portable version too? (https://portableapps.com/apps/utilities/freecommander_portable)

The salient feature for your question, however, is the ability to modify the viewed columns to include one called "Length of the path". This provides a numeric column representing the length of the filename plus the length of the path to the filename (so this would be a constant in the same directory). Sort by this column and you have sorted by filename length!

This is an essential program for Windows, IMHO, and one to which I am more than happy to donate. Marek has done an incredible job developing, updating and maintaining this application, and I applaud him.


If you just want the list of filenames, you can do it easily with excel. Or you can do it with FileBoss . Windows-explorer can't do this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.