Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 22, 2012, 01:30 PM   #1
farmerdoug
macrumors 6502a
 
Join Date: Sep 2008
open lots of files without using filenames

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.
farmerdoug is offline   0 Reply With Quote
Old Oct 22, 2012, 01:32 PM   #2
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: 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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Oct 22, 2012, 01:40 PM   #3
chown33
macrumors 603
 
Join Date: Aug 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.

http://en.wikipedia.org/wiki/Dirent.h
chown33 is offline   0 Reply With Quote
Old Oct 22, 2012, 09:07 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Oct 23, 2012, 11:17 AM   #5
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by farmerdoug View Post
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)
mrichmon is offline   0 Reply With Quote
Old Oct 23, 2012, 11:44 AM   #6
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
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).
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Oct 23, 2012, 01:00 PM   #7
ytk
macrumors regular
 
Join Date: Jul 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.
ytk is offline   0 Reply With Quote
Old Oct 24, 2012, 05:55 AM   #8
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 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.
farmerdoug is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Can't screenshot, open Skype, delete files, open itunes.... delab99 OS X 12 Dec 19, 2013 01:45 PM
Copying lots of subfolders skipping files that already exist? peanutismint OS X 0 Nov 7, 2013 09:14 AM
Deleting lots of Time Machine files aswine Mac OS X 10.7 Lion 5 Feb 22, 2013 03:12 AM
iPhone: Filenames for theme image files FredrikHagen Jailbreaks and iOS Hacks 0 Feb 4, 2013 05:43 PM
How to deal with lots of duplicate files? stanw Mac OS X 10.7 Lion 2 Jan 3, 2013 02:42 AM

Forum Jump

All times are GMT -5. The time now is 05:26 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps