SoFunction
Updated on 2025-04-11

Summary of methods for moving files under ios

This objective c code is used to move files under a specified path

Copy the codeThe code is as follows:

if ([fileManager copyItemAtPath:@"FilePath1"
  toPath:@"FilePath2"  error:NULL]) {
     NSLog(@"Copied successfully");
  }

Method 2

Using NSFileManager:
Make your document path and your cache path. Iterate through all files and move them using NSFileManager

Copy the codeThe code is as follows:

- (void) moveAllDocs {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSString *sourceDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *destinationDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:sourceDirectory error:&error];
    for(NSString *sourceFileName in contents) {
        NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:sourceFileName];
        NSString *destFile = [destinationDirectory stringByAppendingPathComponent:sourceFileName];
        if(![fileManager moveItemAtPath:sourceFile toPath:destFile error:&error]) {
            NSLog(@"Error: %@", error);
        }
    }
}

Method 3

FCFileManager is an iOS file management tool built on top of NSFileManager, simplifying file management. It provides many static methods for performing the most commonly used operations with several lines of code. It works by default file directories, allowing relative paths to be used, but it works easily in any other directory.

Move file:

Copy the codeThe code is as follows:

[FCFileManager moveItemAtPath:@"" toPath:@"tests/"];

Remove file:

Copy the codeThe code is as follows:

//remove file at the specified path
[FCFileManager removeItemAtPath:@""];

The above is the entire content of this article, I hope you like it.