[英]How to rename files/folders from document folder programmatically in iOS
I am working on functionality to save video files in document folder of application in iOS. 我正致力于将视频文件保存在iOS应用程序的文档文件夹中。
How to rename some files programmatically ? 如何以编程方式重命名某些文件 ?
try this code : 试试这段代码:
NSError * err = NULL;
NSFileManager * fm = [[NSFileManager alloc] init];
BOOL result = [fm moveItemAtPath:@"/tmp/test.tt" toPath:@"/tmp/dstpath.tt" error:&err];
if(!result)
NSLog(@"Error: %@", err);
other wise use this method to rename file 另外明智地使用此方法重命名文件
- (void)renameFileWithName:(NSString *)srcName toName:(NSString *)dstName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathSrc = [documentsDirectory stringByAppendingPathComponent:srcName];
NSString *filePathDst = [documentsDirectory stringByAppendingPathComponent:dstName];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePathSrc]) {
NSError *error = nil;
[manager moveItemAtPath:filePathSrc toPath:filePathDst error:&error];
if (error) {
NSLog(@"There is an Error: %@", error);
}
} else {
NSLog(@"File %@ doesn't exists", srcName);
}
}
There is no Direct API to rename the file . 没有Direct API来重命名文件。 Though when you move the file from one place to another place, if that file in destination path not exists then iOS will create the file in the given name.
虽然当您将文件从一个位置移动到另一个位置时,如果目标路径中的该文件不存在,则iOS将以给定名称创建该文件。 For file you can just give the New name of your file, how it should be displayed/referenced.
对于文件,您只需提供文件的新名称,如何显示/引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.