Mac open lots of files without using filenames

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,993
30
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
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?

-Lee
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2
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?
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)
 

whooleytoo

macrumors 604
Aug 2, 2002
6,568
638
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).
 

ytk

macrumors regular
Jul 8, 2010
226
8
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.
 

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.