open lots of files without using filenames

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

  1. macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    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

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    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 603

    Joined:
    Aug 9, 2009
    #3
    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.

    http://en.wikipedia.org/wiki/Dirent.h
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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?

    -Lee
     
  5. macrumors 6502a

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

    Code:
    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
                scan_dirs(current)
            else:
                process_file(current)
    
    if __name__ == "__main__":
        scan_dirs(path)
     
  6. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #6
    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

    Joined:
    Jul 8, 2010
    #7
    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

    Joined:
    Sep 16, 2008
    #8
    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