Objective-C學習-尋找檔案

來源:互聯網
上載者:User

//  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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.