黑马程序员技术交流社区
标题:
iOS 创建文件夹,删除文件夹,对文件夹重命名的操作
[打印本页]
作者:
caizexu
时间:
2015-11-28 09:28
标题:
iOS 创建文件夹,删除文件夹,对文件夹重命名的操作
<div><span style="font-weight: bold;"><font size="5" face="微软雅黑">
</font></span></div><div style="text-align: left;"><p> <font size="3">+ (void)createFolder:(NSString *)folderName {</font></p>
<p><font size="3"> NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@",
NSHomeDirectory(),folderName];</font></p>
<p><font size="3"> NSLog(@"HomeDir: %@",imageDir);</font></p>
<p><font size="3"> BOOL isDir = NO;</font></p>
<p><font size="3"> NSFileManager *fileManager = [NSFileManager defaultManager];</font></p>
<p><font size="3"> BOOL existed = [fileManager fileExistsAtPath:imageDir
isDirectory:&isDir];</font></p>
<p><font size="3"> if ( !(isDir == YES && existed == YES) )</font></p>
<p><font size="3"> {</font></p></div>
<div style="text-align: center;">
</div><div>
<p><font size="3"> [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES
attributes:nil error:nil];</font></p>
<p><font size="3"> }</font></p>
<p><font size="3"> }</font></p></div>
<p><font size="3"> NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@",
NSHomeDirectory(),folderName];</font></p>
<p><font size="3"> NSLog(@"HomeDir: %@",imageDir);</font></p>
<p><font size="3"> NSFileManager *fileManager = [NSFileManager defaultManager];</font></p>
<p><font size="3"> [fileManager removeItemAtPath:imageDir error:nil];</font></p>
<p><font size="3"> }</font></p>
<p><font size="3"> NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@",
NSHomeDirectory(),folderName];</font></p>
<p><font size="3"> NSLog(@"HomeDir: %@",imageDir);</font></p>
<p><font size="3"> NSFileManager *fileManager = [NSFileManager defaultManager];</font></p>
<p><font size="3"> [fileManager removeItemAtPath:imageDir error:nil];</font></p>
<p><font size="3"> }</font></p>
<p><font size="3"> + (void)changeFolderName:(NSString *)folderName beforeName:(NSString
*)beforeName {</font></p>
<p><font size="3"> NSFileManager *fm = [NSFileManager defaultManager];</font></p>
<p><font size="3"> NSString *beforeFolder = [NSString stringWithFormat:@"%@/Documents/%@",
NSHomeDirectory(),beforeName];</font></p>
<p><font size="3"> NSString *afterFolder = [NSString stringWithFormat:@"%@/Documents/%@",
NSHomeDirectory(),folderName];</font></p>
<p><font size="3"> [fm createDirectoryAtPath:afterFolder withIntermediateDirectories:YES
attributes:nil error:nil];</font></p>
<p><font size="3"> NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:beforeFolder];</font></p>
<p><font size="3"> NSString *path;</font></p>
<p><font size="3"> while ((path = [dirEnum nextObject]) != nil) {</font></p>
<p><font size="3"> [fm moveItemAtPath:[NSString
stringWithFormat:@"%@/%@",beforeFolder,path]</font></p>
<p><font size="3"> toPath:[NSString stringWithFormat:@"%@/%@",afterFolder,path]</font></p>
<p><font size="3"> error:NULL];</font></p>
<p><font size="3"> }</font></p>
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2