refer to Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSArray.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *path;
NSFileManager *fm;
NSDirectoryEnumerator *dirEnum;
NSArray *dirArray;
// Need to create an instance of the file manager
fm = [NSFileManager defaultManager]; //所有檔案處理要先定義的動作
// Get current working directory path
path = [fm currentDirectoryPath];
// Enumerate the directory
dirEnum = [fm enumeratorAtPath: path]; //會連次目錄的檔案一併列舉
NSLog (@ ” Contents of %@: ” , path);
while ((path = [dirEnum nextObject]) != nil)
NSLog (@ ” %@ ” , path);
// Another way to enumerate a directory
dirArray = [fm directoryContentsAtPath: [fm currentDirectoryPath]];//只就純檔案
NSLog (@ ” Contents using directoryContentsAtPath: ” );
for ( path in dirArray )
NSLog (@ ” %@ ” , path);
[pool drain];
return 0;
}
Contents of /Users/stevekochan/mysrc/ch16:
a.out
dir1.m
dir2.m
file1.m
newdir
newdir/file1.m
newdir/output
path1.m
testfile
Contents using directoryContentsAtPath:
a.out
dir1.m
dir2.m
file1.m
newdir
path1.m
testfile
沒有留言:
張貼留言