1

Can I sort files according to the length of filename?

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

5 Answers 5

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
1
  • 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;) Jan 7, 2011 at 6:14
2

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
1
  • How does this work? I can't really comprehend what you're doing here
    – Jan Berndt
    May 16, 2020 at 11:36
2

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

1

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.

0

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 :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .