2017-02-13 127 views
0

我已經解壓縮文件,發送到我的文檔目錄:如何檢索特定文件擴展名的文件的文件路徑?

[SSZipArchive unzipFileAtPath:path toDestination:destinationPath]; 

在解壓文件將有5種不同類型的文件。我只想知道擴展名爲'.shp'的文件的路徑和文件名。

我已經試過如下:

NSString *filePath = [[NSBundle mainBundle] pathForResource:destinationPath ofType:@"shp"]; 

之後,我想刪除該文件夾中的文件的所有內容。

任何想法?提前致謝。

+0

請檢查我的答案,讓我知道,如果這是你在找什麼 – KrishnaCA

回答

1

首先,獲取所有目錄文件。

NSString *bundle = [[NSBundle mainBundle] bundlePath]; 
NSFileManager * aMan = [NSFileManager defaultManager]; 
NSArray * allFiles = [aMan contentsOfDirectoryAtPath: bundle]; 

則可以用以下的方法之一來篩選所需的擴展:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.shp'"]; 
NSArray *filtered = [allFiles filteredArrayUsingPredicate:filter]; 

下一頁 - 刪除文件,循環過濾。但它對我來說並不好。
所以,我更喜歡這一個:

NSError *error; 
for (NSString * elem in allFiles) { 
    if ([[elem pathExtension] isEqualToString:@"shp"]) 
     [aMan removeItemAtPath:[bundle stringByAppendingPathComponent:elem] error:&error]; 

希望,它有助於

-1
find . -iname "*.shp" 

這裏'。'代表在當前和它的子文件夾中搜索。您甚至可以指定目錄路徑而不是'。',您要在其中搜索。或者,如果您搜索整個根目錄,則可以使用「/」。 '-i'表示擴展名不區分大小寫。 '-iname'用於不區分大小寫匹配文件名,並將文件名指定爲「* .shp」,其中*與任何字符匹配,名稱應以.shp結尾。

+0

等待這是在Objective-C? –

+0

我想這就是你要找的東西 - http://stackoverflow.com/questions/14110165/file-search-with-specific-extension-objective-c – Ajax1986

0

可以使用的NSFileManager刪除文件:

NSFileManager * fileManager = [[NSFileManager alloc]init]; 

[fileManager removeItemAtPath:@"your path" error:nil]; 
+0

謝謝!但我不能給這個答案部分信用:第 –

0

你可以做到這一點通過以下方式:

NSURL *baseURL = [NSURL URLWithString:destinationPath]; 
NSDirectoryEnumerator* filesEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:baseURL includingPropertiesForKeys:@[] options:0 errorHandler:nil]; 

NSURL* fileURL; 
while (fileURL = [filesEnumerator nextObject]) { 
    NSString* file = [fileURL lastPathComponent]; 
    BOOL match = [file containsString:@".shp"]; 
    if (match) { 
     [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil]; 
    } 
} 

請讓我知道這是否解決了問題..

相關問題