read directory in alpha order - SCO

This is a discussion on read directory in alpha order - SCO ; 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 ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: read directory in alpha order

  1. read directory in alpha order

    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

  2. Re: read directory in alpha order

    On 2008-03-28, migurus wrote:
    > 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
    andrews@sdf.lonestar.org

  3. Re: read directory in alpha order

    On Mar 28, 1:49*pm, Andrew Smallshaw wrote:
    > On 2008-03-28, migurus wrote:
    >
    > > 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
    > andr...@sdf.lonestar.org


    Thank you, I will appreciate the files you have mentioned.
    Mi

  4. Re: read directory in alpha order

    On Fri, Mar 28, 2008, migurus wrote:
    >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: bill@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.

+ Reply to Thread