84467715 2011-10-31
// ocStudy
// main.m
//
// 从主目录开始,找出所有扩展名为jpg的文件的路径
// Created by mark on 11-10-30.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSFileManager *manger;
manger =[NSFileManager defaultManager];//建立一个文件管理器
NSString *home;
home= [@"~" stringByExpandingTildeInPath];//~是一个代表主目录的速记符,这个方法调用是指定主目录到home
NSMutableArray *file;
file = [NSMutableArray arrayWithCapacity: 42];//新建一个可变数组,42是随便填的,不影响数组扩展,你也可以写41,随你喜好
for (NSString *filename in [manger enumeratorAtPath: home]){//遍历每个文件路径,找出扩展名为jpg的文件,比起建立两个枚举器(下面注释部分)用快速枚举方法能更轻松快捷地访问数组的每个元素
if ([[filename pathExtension]
isEqualTo: @"jpg"]) {
[file addObject: filename];
}
}
for (NSString *filename in file ) {
NSLog(@"%@", filename);
}
// NSDirectoryEnumerator *direnum;
// direnum = [manger enumeratorAtPath: home];
//
//
// NSString *filename;
// while (filename = [direnum nextObject]) {
// if ([[filename pathExtension] isEqualTo: @"jpg"]) {
// [file addObject: filename];
// }
// }
//
// NSEnumerator *fileenum;
// fileenum = [file objectEnumerator];
//
// while (filename = [fileenum nextObject]) {
// NSLog(@"%@" , filename);
// }
}
return 0;
}
输出如下(部分):
2011-10-30 04:45:55.104 ocStudy[309:707] Desktop/未命名_meitu_1.jpg
2011-10-30 04:45:55.105 ocStudy[309:707] Desktop/软件及驱动/QQ.app/Contents/Resources/guide1.jpg
2011-10-30 04:45:55.106 ocStudy[309:707] Desktop/软件及驱动/QQ.app/Contents/Resources/guide2.jpg
2011-10-30 04:45:55.106 ocStudy[309:707] Desktop/软件及驱动/QQ.app/Contents/Resources/guide3.jpg
2011-10-30 04:45:55.107 ocStudy[309:707] Desktop/软件及驱动/QQ.app/Contents/Resources/guide4.jpg
2011-10-30 04:45:55.107 ocStudy[309:707] Desktop/软件及驱动/QQ.app/Contents/Resources/guide5.jpg
2011-10-30 04:45:55.108 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F00/GKTL.jpg
2011-10-30 04:45:55.108 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F00/YDFF.jpg
2011-10-30 04:45:55.109 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F01/AJFW.jpg
2011-10-30 04:45:55.109 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F02/PXMW.jpg
2011-10-30 04:45:55.110 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F02/TBIY.jpg
2011-10-30 04:45:55.110 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F05/JIJV.jpg
2011-10-30 04:45:55.111 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F05/MUGA.jpg
2011-10-30 04:45:55.111 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F05/NXON.jpg
2011-10-30 04:45:55.112 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F06/JRWJ.jpg
2011-10-30 04:45:55.112 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F06/UQTD.jpg
2011-10-30 04:45:55.113 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F07/GWGQ.jpg
2011-10-30 04:45:55.113 ocStudy[309:707] Library/Application Support/iPhone Simulator/User/Media/Photos/Thumbs/F07/USPA.jpg