【问题标题】:Remove file types from Documents Directory从文档目录中删除文件类型
【发布时间】:2011-08-14 17:11:45
【问题描述】:

我正在尝试删除文档目录中扩展名为“.jpg”的所有文件,经过多次尝试,我仍然没有成功。

我现在的代码是:

-(void)removeOneImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
}

使用此代码,我可以删除一个具有特定名称的文件。

谁能帮我删除所有具有特定扩展名的文件?

亲切的问候,

下雪

【问题讨论】:

    标签: iphone cocoa xcode ipad


    【解决方案1】:
        NSString *extension = @"jpg";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];  
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {
    
        if ([[filename pathExtension] isEqualToString:extension]) {
    
            [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
        }
    }
    

    【讨论】:

      【解决方案2】:
      // Get the Documents directory path
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectoryPath = [paths objectAtIndex:0];
      
      // Delete the file using NSFileManager
      NSFileManager *fileManager = [NSFileManager defaultManager];
      [fileManager removeItemAtPath:[documentsDirectoryPath stringByAppendingPathComponent:yourFile.txt] error:nil];
      

      更多信息

      How to delete ALL FILES in a specified directory on the app?

      【讨论】:

        【解决方案3】:

        很简单的方法:

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *imageName = [NSString stringWithFormat:@"demo.jpg"];
        [fileManager removeItemAtPath:imageName error:NULL];
        

        【讨论】:

          【解决方案4】:

          如果您更喜欢快速枚举:

          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
          NSString *extension = @"jpg";
          
          NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
          
          for (NSString *file in contents) {
              NSString *filePath = [documentsDirectory stringByAppendingPathComponent:file];
          
              if ( ([[file pathExtension] isEqualToString:extension]) && [fileManager isDeletableFileAtPath:filePath] ) {
                  [fileManager removeItemAtPath:filePath error:nil];
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2023-04-03
            • 2011-08-06
            • 2013-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多