taiyang2014

删除某个文件夹下的所有文件

    //删除本地文件夹下保存的文件
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
                    NSString *dicrectoryPath = [docPath stringByAppendingPathComponent:@"Pic"];
                   
                    NSArray *fileList = [[NSArray alloc] init];
                    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
                    fileList = [fileManager contentsOfDirectoryAtPath:dicrectoryPath error:nil];
                    for (NSString *path in fileList) {
                        if ([path containsString:@"png"]) {
                            NSString *filePath = [dicrectoryPath stringByAppendingPathComponent:path];
                            BOOL result1 = [fileManager removeItemAtPath:filePath error:nil];
                            if (result1) {
                                NSLog(@"删除成功");
                            }
                        }
                    }
//创建文件  将图片保存到该路径下
 
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]*1000;
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
   
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *dicrectoryPath = [docPath stringByAppendingPathComponent:@"Pic"];
    NSString *filePath;
    BOOL rel = [fileManager fileExistsAtPath:dicrectoryPath isDirectory:nil];
    if (rel) {
        NSLog(@"存在文件夹");
       
        filePath = [dicrectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.png", [[DataHandle shareDataHandle] getLocalData:USERID], timeString]];
    } else {
        NSLog(@"不存在文件夹");
        NSError *error = nil;
        BOOL result = [fileManager createDirectoryAtPath:dicrectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (result) {
            filePath = [dicrectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.png", [[DataHandle shareDataHandle] getLocalData:USERID], timeString]];
        }
    }
    [data writeToFile:filePath atomically:YES];

posted on 2015-09-30 17:15  taiyang2014  阅读(627)  评论(0编辑  收藏  举报

导航