0

I have unzipped a file and sent to my documents directory:

[SSZipArchive unzipFileAtPath:path toDestination:destinationPath];

In the unzipped file there will be 5 different types of files. I only want to know the path and file name for the file with an extension type of '.shp'.

I've tried the following:

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

Afterwards, I would like to delete all the contents of the files in that folder.

Any ideas? Thanks in advance.

butter_baby
  • 858
  • 2
  • 10
  • 24

3 Answers3

1

First, get all directory files.

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

Then it is possible to filter needed extension with one of following methods:

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

Next - delete files, looping filtered. But it looks not good for me.
So, I prefer this one:

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

Hope, it helps

ETech
  • 1,613
  • 16
  • 17
0

You can use NSFileManager to remove the files:

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

[fileManager removeItemAtPath:@"your path" error:nil];
aircraft
  • 25,146
  • 28
  • 91
  • 166
0

You can do it in the following way:

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];
    }
}

Please let me know if it resolves the problem..

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31