#1 20 Feb 2021 04:23

Registered: 17 Apr 2016
Posts: 85

Is there a better option than DIR or LS for listing files?

Many years ago, on the Amiga, I recall having a Dir/LS command that had a staggering number of options. You could format the output in pretty much any way you could think of.

I've tried all the options in Dir and I've even downloaded a couple different ports of the Unix LS command, but none of them will do exactly what I want.

Ideally, I'd like it to recursively list all the files in all the sub-directories with the size in bytes listed first, followed by the relative path and filename. Like this;

    49,152  Test1\Stuff.txt
     4,096  Test2\Junk.txt
12,121,369  Videos\Demo.mp4

However I can't find a program that will list the relative path name in front of the filename. They all list the directories separately, then list the files that are in them.


for /r %%F in (*.*) do echo %%~zF %%F

Prints the sizes without commas and not neatly aligned, plus it prints the full path including the drive letter, which I do not want.

With either method, I'm going to have to jump through a ton of hoops to try and reformat the list the way I want it. It would be so much easier if there was a third-party Dir command that let you format the output how you wanted.

Does such a thing exist?


#2 21 Feb 2021 11:06

Registered: 15 Oct 2020
Posts: 9

Re: Is there a better option than DIR or LS for listing files?

the dir command has options for sorting and grouping output, and when iterated over from within a for /f loop you can strip the drive letter by just using the ~p modifier.

/A: is the atrribute Switch,subswitch '-D' denotes NOT directory
/S is the recursive switch of the Dir command
/O: is the switch for Sort order, S is the subswitch to sort by size (smallest first), G is the subswitch to group directories while sorting.

From the command line:

@(For /F "Delims=" %G in ('dir /B /S /A:-D /O:SG')Do @Echo(%~zG %~pG) | @more

~p will effectively only strip 'DriveLetter:' from the path, if you wish to remove path elements up to and including the location of the parent folder from the path string, you'll need to assign the path to a variable and use substring modification to remove the unwanted parts of the path from the string.


Board footer

Powered by FluxBB