PDA

View Full Version : Is there any way to sort files by length of file name?



Bohandas
2016-03-18, 10:59 PM
Is there any way to sort files by length of file name? I'm backing up data and some of the files in a very large folder have names that are too long for any of the file systems that my Blu-Ray burning program can work with. I need to transfer all of these to a zip file with a shorter name, but first I need to find them all, and that's unfeasibly arduous to do manually

factotum
2016-03-19, 03:04 AM
Not easily, no--there's no operating system I've ever seen that lets you sort filenames by length. Only way I can think of to do it would be to export all the filenames to a spreadsheet, create a second column which uses a command to determine the length of the first column, and then sort the whole lot by that second column.

Khedrac
2016-03-19, 03:06 AM
Assuming Windows:

Open up a DOS (Command) shell and change directory to the folder in question

Type: dir > filelist.txt

This will create a file called "filelist.txt" which contains the list of all files in the folder.
Open the text file with a Spreadsheet of your choice in this you can easily use a formula to calculate the filename lenghts and sort accordingly.

You then know which files you need to rename.

Edit: ninja'd

blunk
2016-03-19, 08:44 PM
Here's a unix (sh/bash) one-liner, using length of relative path (not sure if it'll give you what you want if you're using subdirectories):

for f in $(find . -type f); do echo $(echo $f | wc -c) $f; done | sort -n | cut -d ' ' -f 2

Telok
2016-03-19, 11:14 PM
Here's a unix (sh/bash) one-liner, using length of relative path (not sure if it'll give you what you want if you're using subdirectories):

for f in $(find . -type f); do echo $(echo $f | wc -c) $f; done | sort -n | cut -d ' ' -f 2

Hrm. I'd be tempted to $find the entire tree into a file, $awk or $sed it to put a space in front of the last \, then $sort or $awk by what comes after the first space. Your output file ought to then list all the paths, a space, and the (sorter by length) filename.

Someone else can do the details, I have to go write two assembly programs.

blunk
2016-03-19, 11:22 PM
Hrm. I'd be tempted to $find the entire tree into a file, $awk or $sed it to put a space in front of the last \, then $sort or $awk by what comes after the first space. Your output file ought to then list all the paths, a space, and the (sorter by length) filename.

Someone else can do the details, I have to go write two assembly programs.But one-liners are cool and don't leave files around to be forgotten to be deleted!

If you want the length, just leave off the cut. But the OP just said he wanted sorted files :smallsmile:

EDIT: but you're right, I didn't like using a shell-specific loop (or in-memory caching), either. Here it is in one line without files:

find . -type f | awk '{print length($1) " " $1}' | sort -n | cut -d ' ' -f 2

Bohandas
2016-03-20, 12:15 PM
The spreadsheet thing doesn't work for my purposes; the point was to be able to select them all at once. Selecting them one at a time the easiest thing would be to just adjust the width of the file heading in detail view to match the cutoff, and then zip or rename anything whose name doesn't get fully displayed.

a nonny mouse
2018-06-11, 06:21 PM
Is there any way to sort files by length of file name?

Sorry I am so late to the table, but yes, there is a program that offers this, unfortunately only in Windows though. See FreeCommander (check out "freecommander dot com", sorry, this idiotic forum won't let me post links), 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? (see "portableapps dot com"...ugh stupid forum)

The salient feature for your needs, 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/path 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.

tl;dr: FreeCommander for Windows will do what you want, easily and without fuss.

Knaight
2018-06-11, 06:28 PM
It's programmable - you basically need something that reads the names of all the files, counts the length of them, assigns them all a variable, then somehow marks the ones over the cutoff length. Editing the name of all of them seems like the obvious way to do it - instead of [name].[filetype] change it to 00000[name].[filytype], then alphabetize it and grab them from the front.

I'm not sure how easy this would be - input/output and computer permissions aren't my programming specialty (that would be doing math for engineering purposes).

Roland St. Jude
2018-06-12, 12:56 AM
Sheriff: Please don't do thread necromancy.