[Info-vax] Suggestion: Adding a /SORT option to DIRECTORY and F$SEARCH ?

Arne Vajhøj arne at vajhoej.dk
Tue Jan 25 19:21:08 EST 2022


On 1/25/2022 3:50 PM, Dave Froble wrote:
> On 1/25/2022 2:09 PM, Simon Clubley wrote:
>> It would be nice to be able to see the output from a DIRECTORY command
>> optionally sorted by one of largest size, smallest size, oldest date,
>> newest date, etc.
>>
>> Likewise, it would be nice to be able to optionally specify the sort
>> order in which filenames are returned by f$search().
>>
>> Does anyone agree ?
> 
> Already wrote it, in Basic.  Want a copy?

Python code:

from os import listdir, stat
from sys import argv, exit
from time import strftime, localtime

if len(argv) < 4:
     print('Usage: python dir.py <dir> <orderby> <order>')
     exit(1)
sel = lambda f : f[0]
if argv[2].lower() == 'name':
     sel = lambda f : f[0]
if argv[2].lower() == 'size':
     sel = lambda f : f[1].st_size
if argv[2].lower() == 'time':
     sel = lambda f : f[1].st_mtime
rev = False
if argv[3].lower() == 'asc':
     rev = False
if argv[3].lower() == 'desc':
     rev = True

allf = [(f,stat(f)) for f in listdir(argv[1])]
allf.sort(reverse=rev, key=sel)
for f in allf:
     print('%-32s %10d %s' % (f[0], f[1].st_size, strftime('%d-%b-%Y 
%H:%M:%S', localtime(f[1].st_mtime))))

Arne




More information about the Info-vax mailing list