简体   繁体   English

如何在iOS中以编程方式从文档文件夹重命名文件/文件夹

[英]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. 对于文件,您只需提供文件的新名称,如何显示/引用它。

you could check this answer. 你可以查看这个答案。 Good luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式访问Assets文件夹中用于TVOS的文件/文件夹 - Access the files/folders in the Assets folder for TVOS programmatically iOS / Swift 4:如何在不删除文件和文件夹现有目标的情况下复制包含多个包含文件的文件夹的文件夹? - iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination? 在iOS应用上将数百个文件缓存24-48小时:全部在“文档”文件夹中还是在多个文件夹中更好? - Caching hundreds of files on iOS app for 24-48 hours: all in Document folder or better in multiple folders? 播放iOS中从应用程序文档文件夹下载的Widevine Classic文件? - Play downloaded Widevine Classic files from app document folder in iOS? iOS,将文件从收件箱文件夹复制到文档路径 - iOS , Copying files from Inbox folder to Document path 如何以编程方式在ios设备中获取文件列表/上传/重命名/写入/读取文件? - How to get List of files/Upload/rename/write/read files in ios device programmatically? 如何获取iPhone资源文件夹中的文件夹和文件列表? - How get list of folders and files from resource folder in iPhone? 重命名文档目录的文件夹 - Rename the folder of document directory 如何隐藏在ios中的文档目录中创建的文件夹? - How to hide folders created in Document Directory in ios? 如何在iOS捆绑包的文件夹层次结构中包括所有文件,但不包括文件夹本身? - How do I include all files within a folder hierarchy in an iOS bundle, but not include the folders themselves?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM