1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > delete Files and Folders(删除文件 文件夹)

delete Files and Folders(删除文件 文件夹)

时间:2021-11-05 01:08:06

相关推荐

delete Files and Folders(删除文件 文件夹)

1。实现方法:

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

e.g.

@property (nonatomic, strong) NSFileManager *fileManager;

/* 根据给定的路径创建文件夹 */

- (void) createFolder:(NSString *)paramPath{

NSError *error = nil;

if ([self.fileManagercreateDirectoryAtPath:paramPathwithIntermediateDirectories:YES

attributes:nilerror:&error] == NO){

NSLog(@"Failed to create folder %@. Error = %@",paramPath,error);

}

else {

NSLog(@"create folder:%@ in succeed",paramPath);

}

}

/* 在指定的路径下创建文件(在指定的文件夹里创建文件) */

- (void) createFilesInFolder:(NSString *)paramPath{

for (NSUInteger counter = 0; counter < 5; counter++){

NSString *fileName = [NSString stringWithFormat:@"%lu.txt",(unsigned long)counter+1];

NSString *path = [paramPathstringByAppendingPathComponent:fileName];

NSString *fileContents = [NSString stringWithFormat:@"Some text_%lu",(unsigned long)counter+1];

NSError *error = nil;

if ([fileContentswriteToFile:pathatomically:YES

encoding:NSUTF8StringEncodingerror:&error] == NO){

NSLog(@"Failed to save file to %@. Error = %@", path, error);

}

}

NSLog(@"create files in folder succeed");

}

/* 遍历 */

- (void) enumerateFilesInFolder:(NSString *)paramPath{

NSError *error = nil;

NSArray *contents = [self.fileManagercontentsOfDirectoryAtPath:paramPatherror:&error];

if ([contents count] > 0 &&error == nil){

NSLog(@"Contents of path %@ = \n%@", paramPath, contents);

}

else if ([contents count] == 0 &&error == nil){

NSLog(@"Contents of path %@ is empty!", paramPath);

}

else {

NSLog(@"Failed to enumerate path %@. Error = %@", paramPath, error);

}

}

/* 删除 */

- (void) deleteFilesInFolder:(NSString *)paramPath{

NSError *error = nil;

NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:paramPatherror:&error];

if (error == nil){

for (NSString *fileName in contents){

/*filePath:the full path */

NSString *filePath = [paramPathstringByAppendingPathComponent:fileName];

if ([self.fileManagerremoveItemAtPath:filePatherror:&error] == NO){

NSLog(@"Failed to remove item at path %@. Error = %@",fileName,error);

}

}

} else {

NSLog(@"Failed to enumerate path %@. Error = %@", paramPath, error);

}

}

/* 删除文件夹*/

- (void) deleteFolder:(NSString *)paramPath{

NSError *error = nil;

if ([self.fileManagerremoveItemAtPath:paramPath error:&error] == NO){

NSLog(@"Failed to remove path %@. Error = %@", paramPath, error);

}else {

NSLog(@"succeed in remove path %@",paramPath);

}

-(void) actionFilesAndFolders{

self.fileManager = [[NSFileManager alloc] init];

NSString *txtFolder = [NSTemporaryDirectory()stringByAppendingPathComponent:@"txt"];

[self createFolder:txtFolder];

[self createFilesInFolder:txtFolder];

[self enumerateFilesInFolder:txtFolder];

[self deleteFilesInFolder:txtFolder];

[self enumerateFilesInFolder:txtFolder];

[self deleteFolder:txtFolder];

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。