open lots of files without using filenames

Discussion in 'Mac Programming' started by farmerdoug, Oct 22, 2012.

  1. macrumors 6502a

    Sep 16, 2008
    I have a large number of directories with non-sequential numbers for names; Each directory contains a variable number of files - all of the same type. I need to open all the files - one at a time. Can I do this without specifying any of the names?

    I guess I could make a list of all the directories; then use system calls to ls to get lists of the files in each directory; then read the files. Doesn't seem to terrible but I think microsoft C used to have routines that would do this for you.
  2. Moderator emeritus


    Oct 8, 2002
    The Bamboo Forest
    They may have obfuscated it for you, but under the covers all they were doing was a directory traversal. You'll need to do that if it's not already offered via the API.
  3. macrumors 604

    Aug 9, 2009
    Read the man page for the Standard C Library function opendir().

    You may also have to read the header file <dirent.h> to learn the members of struct dirent.
  4. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Or keep it simple and have your program work on a single file at a time, and pass this via command line with the results of find. If your program needs a whole directory or all the files to do its work, you could get the whole list of files and read in this "index" from stdin or a file. Saves you the directory structure traversal (which isn't *that* bad).

    These may not be options here, but just keep in mind that there are many powerful tools you can leverage instead of poorly reinventing them.

    Astronomy, stocks, or something new?

  5. macrumors 6502a

    Jun 17, 2003
    In python, the following would do it:

    import os, glob
    path = '/tmp/foo'
    def process_file(file):
        print "processing file: " + file
        # Do stuff to file
    def scan_dirs(path):
        for current in glob.glob( os.path.join(path, '*') ):
            if os.path.isdir(current):
                print 'processing directory: ' + current
    if __name__ == "__main__":
  6. macrumors 603


    Aug 2, 2002
    Cork, Ireland.
    If I understand correctly, you just want to iterate through directories and perform some operation on each file?

    If so (and you're using Cocoa), you could take a look at NSFileManager; either use the contentsOfDirectoryAtPath:error method, or else the enumeratorAtPath: method (though note that performs a deep iteration, into the sub directories).
  7. ytk
    macrumors regular

    Jul 8, 2010
    Err... you didn't say what exactly you're trying to do, or even what language/environment you're trying to do it in. Is this a Bash script, a C program, or what? What do you mean by "open" them? You need to generate a file handle for each one to be read as a part of some script? Just open them in Finder? Display their contents? Are each of these directories full of files contained within a larger directory? If so, you can just use "*/*" to refer to each of the files.

    If you explain what exactly it is you're trying to do, I'm sure somebody here can figure out how to help you with it.
  8. thread starter macrumors 6502a

    Sep 16, 2008
    Thanks for the help guys. I didn't have response set on immediate so I did it like I said but will look into opendir. I didn't say that this was C so anything that wasn't was out but thanks all the same.

Share This Page