Q
如何重命名目录?
7
A
回答
7
NSString *oldDirectoryPath = @"Type your old directory Path";
NSArray *tempArrayForContentsOfDirectory =[[NSFileManager defaultManager] contentsOfDirectoryAtPath:oldDirectoryPath error:nil];
NSString *newDirectoryPath = [[oldDirectoryPath stringByDeletingLastPathComponent]stringByAppendingPathComponent:newDirectoryname];
[[NSFileManager defaultManager] createDirectoryAtPath:newDirectoryPath attributes:nil];
for (int i = 0; i < [tempArrayForContentsOfDirectory count]; i++)
{
NSString *newFilePath = [newDirectoryPath stringByAppendingPathComponent:[tempArrayForContentsOfDirectory objectAtIndex:i]];
NSString *oldFilePath = [oldDirectoryPath stringByAppendingPathComponent:[tempArrayForContentsOfDirectory objectAtIndex:i]];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error];
if (error) {
// handle error
}
}
15
你试过了吗?
NString *newDirectoryName = @"<new folder name>";
NSString *oldPath = @"<path to the old folder>";
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newDirectoryName];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
if (error) {
NSLog(@"%@",error.localizedDescription);
// handle error
}
1
这始终工作
NSLog (@"Copying download file from %@ to %@", aPath, bPath);
if ([[NSFileManager defaultManager] fileExistsAtPath: bPath]) {
[[NSFileManager defaultManager] removeItemAtPath: bPath
error: &error];
}
if (![[NSFileManager defaultManager] copyItemAtPath: aPath
toPath: bPath
error: &error]){}
if ([[NSFileManager defaultManager] removeItemAtPath: aPath
error: &error]) {}
+0
这一个有复制数据的副作用。如果数据很大,或者如果应用程序在操作过程中崩溃,则可能会导致问题。 – 2012-09-27 10:54:34
5
使用moveItemAtPath应该工作。有时这个目录实际上并没有被“重命名”,而是真的移到了另一个地方。在这种情况下,还需要创建目标路径目录结构。 这里我使用的代码片段效果很好:
-(BOOL)renameDir:(NSString *)dirPath asDir:(NSString *)newDirPath cleanExisting:(BOOL)clean
{
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
if (clean && [fm fileExistsAtPath:newDirPath])
{
[fm removeItemAtPath:newDirPath error:&error];
if (error != nil)
{
NSLog(@"Error while renameDir %@ as %@ :\n%@",dirPath,newDirPath,error);
return NO;
}
}
//Make sure container directories exist
NSString *newDirContainer = [newDirPath stringByDeletingLastPathComponent];
if (![fm fileExistsAtPath:newDirContainer])
{
[fm createDirectoryAtPath:newDirContainer withIntermediateDirectories:YES attributes:nil error:&error];
}
if (error==nil)
{
[fm moveItemAtPath:dirPath toPath:newDirPath error:&error];
}
if (error!=nil)
{
NSLog(@"error while moveItemAtPath : %@",error);
}
return (error==nil);
}
+0
它并没有清除旧的目录..是吗? – 2014-05-09 05:33:25
0
这是重命名好文章,删除和创建文件。
// For error information
NSError *error;
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Rename the file, by moving the file
NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
// Attempt the move
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
// Show contents of Documents directory
NSLog(@"Documents directory: %@",
[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
相关问题
- 1. 如何重命名目录?
- 2. 重命名目录
- 3. 重命名目录
- 4. 如何重命名JTree中的目录?
- 5. 如何递归地重命名目录
- 6. 如何通过sysmlink重命名目录?
- 7. 重命名目录的PHP
- 8. SAS:重命名宏目录
- 9. 重命名工作目录
- 10. PHP:如何重命名父目录可变的目录?
- 11. Hadoop HDFS命令 - 重命名目录
- 12. 递归地重命名目录名称
- 13. 通过目录名称重命名目录中的文件
- 14. 在多个目录中的文件重命名为目录名
- 15. 重命名/目录中创建的PDXObjectForm
- 16. 重命名从Openshift克隆git目录
- 17. Bash脚本:重命名输出目录
- 18. 在C++中重命名一个目录
- 19. Bash:拒绝重命名目录
- 20. 重命名目录或文件
- 21. C# - 重命名目录的方法
- 22. Php rename()无法重命名unix目录
- 23. 重命名目录的一部分
- 24. PHP重命名目录失败
- 25. 目录重命名异常在C#
- 26. htaccess帮助重命名目录
- 27. 无法重命名目录powershell
- 28. Symfony的2 web目录重命名
- 29. 重命名目录的递归在Python
- 30. 备份目录+使用bash重命名
道歉,如果我错过了什么地方,但你不需要删除旧的目录吗? – 2011-02-24 00:54:43
@Peter Johnson:如果你不需要删除你的旧目录,那么你可以通过替换下面的代码来使用相同的代码:[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error];到[[NSFileManager defaultManager] copyItemAtPath:oldFilePath toPath:newFilePath error:&error] ;. – iPhoneDv 2011-02-25 06:19:57
马克斯罗斯提出的答案要简单得多,不太复杂。 – 2011-10-14 15:22:18