【问题标题】:How to delete all files inside a folder whose file size is less then a certain size (Bytes)如何删除文件大小小于一定大小(字节)的文件夹中的所有文件
【发布时间】:2015-01-20 17:54:58
【问题描述】:

我有一个名为“已录制”的文件夹。里面说,现在有 10 个音频文件 (.m4a)。这些文件的大小(字节)可能不同或相同。现在我想删除那个文件夹中那些小于542 Bytes的文件。

我可以从发送 "fileName" 的文件夹中删除文件:

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

但我想删除那些文件大小小于542 Bytes 的“已记录”中的文件。 如果您理解我的问题,请回复我。 非常感谢。

【问题讨论】:

    标签: ios objective-c nsurl nsfilemanager


    【解决方案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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2013-11-23
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多