NSDocumentDirectory沙盒中文件的复制、删除

遍历沙盒中的所有文件

  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSArray * paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径
  NSDirectoryEnumerator * myDirectoryEnumerator;
  myDirectoryEnumerator=  [fileManager enumeratorAtPath:documentsDirectory];
  
  while (documentsDirectory = [myDirectoryEnumerator nextObject]) {
    for (NSString * namePath in documentsDirectory.pathComponents) {
      NSLog(@"-----namePath-----%@", namePath  );
    }
  }

操作App分享的文件

App分享的文件存放在DocumentDirectory中的 Inbox/文件夹里面,Inbox/的权限是可读可删除,但是不可用NSFileManager写入文件

将Inbox/文件夹中的文件copy到自己创建的文件夹中
+ (void)copyInboxFilesToNoticeDir{
    //读取copy到inbox的文件
 NSString *documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *inboxFilePath = [documentDir stringByAppendingPathComponent:@"Inbox/"];
    NSDirectoryEnumerator *inboxArray = [[NSFileManager defaultManager] enumeratorAtPath:inboxFilePath];
    for (NSString *fileName in inboxArray){
      NSString *tmpPath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Inbox/%@",fileName]];
      NSString *copyPath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Notice/Attachment/%@",fileName]];
      if([[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:copyPath error:nil]){
        [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
      }
    }
}
移除Inbox文件夹中的所有文件
+ (void)removeInboxFiles{
 NSString *documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *inboxFilePath = [documentDir stringByAppendingPathComponent:@"Inbox/"];
    NSDirectoryEnumerator *inboxArray = [[NSFileManager defaultManager] enumeratorAtPath:inboxFilePath];
    for (NSString *fileName in inboxArray){
      NSString *tmpPath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Inbox/%@",fileName]];
      [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
    }
}
删除某一个文件
+ (void)deleteNoticeAttachment:(NSString *)fileName
{
  [self checkFolderWithName:[NSString stringWithFormat:@"Notice/Attachment"]];
 NSString *documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  NSString *filePath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Notice/Attachment/%@",fileName]];
  if ([[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  }
}


+ (void)checkFolderWithName:(NSString *)folderName
{
 NSString *documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  NSString *folderPath = [documentDir stringByAppendingPathComponent:folderName];
  if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])
  {
    [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
  }
}

你可能感兴趣的:(NSDocumentDirectory沙盒中文件的复制、删除)