Sorting files according to the length of filename

6,937

Solution 1

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

Solution 2

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

#!C:/Perl/bin/perl.exe
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;

foreach(@a)
{
    print "$_\n" if -f;
}

output:

p.pl
p.php
lwp.pl
test.bat
index.htm
index.php
readnsort.pl
scrape_parse.txt

Solution 3

Using PowerShell:

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

https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/sort-object

Solution 4

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.

Share:
6,937

Related videos on Youtube

user55450
Author by

user55450

Updated on September 17, 2022

Comments

  • user55450
    user55450 over 1 year

    Can I sort files according to the length of filename?

    • Dzung Nguyen
      Dzung Nguyen over 13 years
      you should provide your platform ?
    • John T
      John T over 13 years
      @nXqd see tags.
  • user1686
    user1686 over 13 years
    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;)
  • CheeseCrustery
    CheeseCrustery almost 4 years
    How does this work? I can't really comprehend what you're doing here