iOS 文件路径 路径创建 删除

2019-10-17  本文已影响0人  woo_5857

//    return NSTemporaryDirectory() ; //tmp 路径
//    NSString *home = NSHomeDirectory();//获取沙盒路径//    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];

//获取Document文件

    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString* rarFilePath = [docsdirstringByAppendingPathComponent:@"RecordToolTemporary"];//将需要创建的串拼接到后面

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOLisDir =NO;

    // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录

    BOOLexisted = [fileManagerfileExistsAtPath:rarFilePathisDirectory:&isDir];

    if( !(isDir ==YES&& existed ==YES) ) {//如果文件夹不存在

        [fileManagercreateDirectoryAtPath:rarFilePath withIntermediateDirectories:YES attributes:nil error:nil];

    }

    return[NSStringstringWithFormat:@"%@/",rarFilePath];

获取文件夹下某个后缀的所有文件

获取ios_patch文件夹下面pat后缀的所有文件

NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *baseSavePath = [documentPath stringByAppendingString:@"/IOSBundle/ios_patch/"];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSDirectoryEnumerator*myDirectoryEnumerator = [fileManagerenumeratorAtPath:baseSavePath];  //baseSavePath 为文件夹的路径

NSMutableArray *filePathArray = [[NSMutableArray alloc]init];  //用来存目录名字的数组

NSString *file;

while((file=[myDirectoryEnumerator nextObject]))    //遍历当前目录

{

    if([[file pathExtension] isEqualToString:@"pat"])  //取得后缀名为.pat的文件名

    {

        [filePathArray addObject:[baseSavePath stringByAppendingPathComponent:file]];//存到数组

    }

}

NSString*patch1Path = [filePathArraylastObject];

删除文件夹下面的所有内容

删除IOSBundle文件夹下的所有文件

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];

NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:bundlePath];

NSString*fileName;

while(fileName= [dirEnum nextObject]) {

    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",path,fileName] error:nil];

}

删除文件路径(文件夹)

NSFileManager *fileManage = [NSFileManager defaultManager];

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];

NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];

[fileManage removeItemAtPath:bundlePath error:nil];

根据文件名在某个路径下获取文件

NSString * appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];

NSString*baseBundleSavePath = [appDir stringByAppendingString:@"/BaseBundle/output/ios/bundle"];

NSString *docPath = [NSBundle pathForResource:@"main" ofType:@"jsbundle" inDirectory:baseBundleSavePath];

创建文件路径(文件夹)

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];

NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"IOSBundle"];

//是否存在文件夹IOSBundle,不存在则创建

if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {

    [[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:nil];

}

删除文件夹下面后缀结尾的文件

NSString *extension =@"png";

NSFileManager*fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString*documentsDirectory = [pathsobjectAtIndex:0];

NSArray*contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

NSEnumerator*enumerator = [contentsobjectEnumerator];

NSString*filename;

while((filename = [enumerator nextObject])) {

    if([[filename pathExtension] isEqualToString:extension]) {

        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:nil];

        }

/**

 文件拷贝到文件夹

 @paramtoPath 拷贝文章

 @paramfromePath 来源

 */

+ (void)fileCopyToPath:(NSString*)toPath fromePath:(NSString*)fromePath {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOLisSuccess = [fileManagercopyItemAtPath:fromePathtoPath:toPatherror:nil];

    returnisSuccess;

}

上一篇 下一篇

猜你喜欢

热点阅读