拷贝指定文件夹里的内容到指定目录下
程序员文章站
2024-03-09 09:34:17
...
/**
拷贝指定文件夹里的内容到指定目录下
@param sourcePath 源文件夹
@param toPath 目标文件夹
@return 是否完成拷贝
*/
-(BOOL)copyFileFromPath:(NSString *)sourcePath toPath:(NSString *)toPath
{
BOOL copySucceed = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:toPath]) {
NSError *error = nil;
[fileManager createDirectoryAtPath:toPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
//TODO 创建指定拷贝目录时候失败
}
}
NSArray* array = [fileManager contentsOfDirectoryAtPath:sourcePath error:nil];
for(int i = 0; i<[array count]; i++)
{
NSString *fullPath = [sourcePath stringByAppendingPathComponent:[array objectAtIndex:i]];
NSString *fullToPath = [toPath stringByAppendingPathComponent:[array objectAtIndex:i]];
//判断是不是文件夹
BOOL isFolder = NO;
//判断是不是存在路径 并且是不是文件夹
BOOL isExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isFolder];
if (isExist)
{
NSError *err = nil;
[[NSFileManager defaultManager] copyItemAtPath:fullPath toPath:fullToPath error:&err];
if (err) {
copySucceed=NO;
}
if (isFolder)
{
[self copyFileFromPath:fullPath toPath:fullToPath];
}
}
}
return copySucceed;
}