Discussion:
read directory in alpha order
(too old to reply)
migurus
2008-03-28 19:19:37 UTC
Permalink
I need to read directory tree in an alphabetical order. The opendir/
readdir functions work great, but do not have any ordering.

Is there some other means to read it in alphabetical order? something
like scandir on Linux?

I don't want to reinvent the wheel and start doing my own resorting if
it is done already.

My environment is OSR 507, MP3, MP5, OpenServer devel system 5.2.0
Any suggestions?

Also, I do have GNU Tools ver 5.0.7g installed, but would much raher
stay within OpenServer devel environment.

Regards
Mi
Andrew Smallshaw
2008-03-28 20:49:56 UTC
Permalink
Post by migurus
I need to read directory tree in an alphabetical order. The opendir/
readdir functions work great, but do not have any ordering.
Is there some other means to read it in alphabetical order? something
like scandir on Linux?
I don't want to reinvent the wheel and start doing my own resorting if
it is done already.
scandir() is a BSDism and not an official standard. It shouldn't
be too difficult to roll your own if you need a direct equivalent,
even easier if you only ever need sorted directories and don't mind
changing the interface.

Alternatively, borrow it from one of the BSDs (avoid Linux unless
you are going to GPL your program). I've had a look at the source
on this NetBSD system and scandir() is only about 70 lines which
look reasonably portable with only minor tweaks. The related
alphasort() is nothing more than a wrapper around strcmp().

I'll send you a copy of the relevant files via email in case you
are interested.
--
Andrew Smallshaw
***@sdf.lonestar.org
migurus
2008-03-28 21:28:32 UTC
Permalink
Post by migurus
I need to read directory tree in an alphabetical order. The opendir/
readdir functions work great, but do not have any ordering.
Is there some other means to read it in alphabetical order? something
like scandir on Linux?
I don't want to reinvent the wheel and start doing my own resorting if
it is done already.
scandir() is a BSDism and not an official standard.  It shouldn't
be too difficult to roll your own if you need a direct equivalent,
even easier if you only ever need sorted directories and don't mind
changing the interface.
Alternatively, borrow it from one of the BSDs (avoid Linux unless
you are going to GPL your program).  I've had a look at the source
on this NetBSD system and scandir() is only about 70 lines which
look reasonably portable with only minor tweaks.  The related
alphasort() is nothing more than a wrapper around strcmp().
I'll send you a copy of the relevant files via email in case you
are interested.
--
Andrew Smallshaw
Thank you, I will appreciate the files you have mentioned.
Mi
Bill Campbell
2008-03-28 22:56:37 UTC
Permalink
Post by migurus
I need to read directory tree in an alphabetical order. The opendir/
readdir functions work great, but do not have any ordering.
Is there some other means to read it in alphabetical order? something
like scandir on Linux?
I don't want to reinvent the wheel and start doing my own resorting if
it is done already.
My environment is OSR 507, MP3, MP5, OpenServer devel system 5.2.0
Any suggestions?
I generally do this sort of thing in python although one could do
it in a shell with gnu-find's ``-maxdepth'', sort, etc.

This is untested as I'm composing in e-mail.

#!/usr/bin/env python
import os, sys

def procdir(dir):
entries = os.readdir(dir)
entries.sort()
for entry in entries:
p = os.path.join(dir, entry)
if os.path.isdir(p) and not os.path.islink(p):
procdir(p)
else:
# do stuff here

for arg in sys.argv[1:]:
procdir(arg)

sys.exit(0)

Bill
--
INTERNET: ***@celestial.com Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676

Guns are no more responsible for killing people than the spoon is
responsible for making Rosie O'Donnell fat.

Loading...