2014-11-22 123 views
0

我有一個文件夾調用「Recorded」。裏面說,現在有10個音頻文件(.m4a)。這些文件(字節)的大小可能不同或相同。現在我想刪除那個文件夾內的文件,其大小小於542 Bytes如何刪除文件大小小於某個特定大小的文件夾中的所有文件(字節)

我可以從該文件夾發送「文件名」刪除文件:

- (void)removeAudioFile:(NSString *) fileName 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    NSString *folder = [documentsPath stringByAppendingPathComponent:@"/Recorded"]; 
    NSString *filePath = [folder stringByAppendingPathComponent:fileName]; 
    NSError *error; 
    BOOL success = [fileManager removeItemAtPath:filePath error:&error]; 
    if (success) 
    { 

    } 
    else 
    { 
     NSLog(@"Could not delete file -:%@ ",[error localizedDescription]); 
    } 
} 

我可以用來衡量該文件夾內的特定文件大小:

-(void) FileSize:(NSString *) urlPath 
{ 
    NSError *attributesError; 
    NSString *path = [urlPath stringByAppendingString:@"/22Nov2014_02.19.50AM.m4a"]; 

    unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:&attributesError] fileSize]; 

    NSLog(@"file size %lld", fileSize); 
} 

我可以在裏面刪除所有文件該文件夾:

-(void) deleteAllFiles:(NSString *) urlPath 
{ 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:urlPath error:nil]; 
    for (NSString *filename in fileArray) 
    { 
     [fileMgr removeItemAtPath:[urlPath stringByAppendingPathComponent:filename] error:NULL]; 
    } 
} 

但是我想要刪除e文件大小小於542 Bytes的「已錄製」內的文件。 如果您瞭解我的問題,請回復我。 非常感謝先進。

回答

1

簡單的檢查,如果文件大小爲542個字節,如果是這樣刪除它:

-(void) deleteAllFiles:(NSString *) urlPath 
{ 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:urlPath error:nil]; 
    for (NSString *filename in fileArray) 
    { 
     NSString *filePath = [path stringByAppendingPathComponent:filename]; 
     unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize]; 
     if (fileSize < 542) [fileMgr removeItemAtPath:filePath error:NULL]; 
    } 
} 
+0

感謝的人。它正在工作。完善。 :) 祝您有美好的一天。 – Tulon 2014-11-22 18:50:38