操作文件(取固定结尾的文件路径)(同名文件覆盖)

//
//  ViewController.m
//  hhhhh
//
//  Created by allan on 2019/5/24.
//  Copyright © 2019 allan. All rights reserved.
//

#import "ViewController.h"
#import "YCDownloadDB.h"

NSFileManager *__fm(void) {
    return [NSFileManager defaultManager];
}

NSString *__docPath(void) {
    
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSFileManager *fm = __fm();
    NSString *docPath = __docPath();
    // 深度搜索
    NSArray<NSString *> *subPaths = [fm subpathsAtPath:docPath];
    NSLog(@"%@\n\n",subPaths);
    NSString *labsSourcePath;
    NSString *jsSourcePath;
    NSString *imgSourcePath;
    NSString *soundSourcePath;
    NSString *fontsSourcePath;
    for (NSString *subPath in subPaths) {
        if ([subPath hasPrefix:[YCDownloadDB tagNameLabs]] && [subPath hasSuffix:[YCDownloadDB tagNameLabs]]) {
            labsSourcePath = subPath;
        }
        
        if ([subPath hasPrefix:[YCDownloadDB tagNameDist]]) {
            if ([subPath hasSuffix:@"js"]) {
                jsSourcePath = subPath;
            }
            if ([subPath hasSuffix:@"img"]) {
                imgSourcePath = subPath;
            }
            if ([subPath hasSuffix:@"sound"]) {
                soundSourcePath = subPath;
            }
        }
        
        if ([subPath hasPrefix:[YCDownloadDB tagNameFonts]] && [subPath hasSuffix:[YCDownloadDB tagNameFonts]]) {
            fontsSourcePath = subPath;
        }
    }
    
    NSString *basePath = docPath;
    
    NSString *js = [basePath stringByAppendingPathComponent:jsSourcePath];
    NSString *img = [basePath stringByAppendingPathComponent:imgSourcePath];
    NSString *sound = [basePath stringByAppendingPathComponent:soundSourcePath];
    
    NSString *labs = [basePath stringByAppendingPathComponent:labsSourcePath];
    
    NSString *fonts = [basePath stringByAppendingPathComponent:fontsSourcePath];
    
    
    NSString *kejian = [docPath stringByAppendingPathComponent:@"kejian"];
    NSString *commonWebPath = [kejian stringByAppendingPathComponent:@"common_web"];
    BOOL isDir;
    if (!([fm fileExistsAtPath:commonWebPath isDirectory:&isDir] && isDir)) {
        NSError *error;
        [fm createDirectoryAtPath:commonWebPath withIntermediateDirectories:YES attributes:nil error:&error];
        NSLog(@"%@", error);
    }
    if ([fm fileExistsAtPath:commonWebPath isDirectory:&isDir] && isDir) {
        NSString *des = [commonWebPath stringByAppendingPathComponent:fonts.lastPathComponent];
        [self coverDes:des fromSource:fonts];
    }
    NSString *desDistPath = [kejian stringByAppendingPathComponent:@"common_web/dist"];
    if (!([fm fileExistsAtPath:desDistPath isDirectory:&isDir] && isDir)) {
        NSError *error;
        [fm createDirectoryAtPath:desDistPath withIntermediateDirectories:YES attributes:nil error:&error];
        NSLog(@"%@", error);
    }
    if ([fm fileExistsAtPath:desDistPath isDirectory:&isDir] && isDir) {
        NSError *error;
        [self coverDes:[desDistPath stringByAppendingPathComponent:js.lastPathComponent] fromSource:js];
        [self coverDes:[desDistPath stringByAppendingPathComponent:img.lastPathComponent] fromSource:img];
        [self coverDes:[desDistPath stringByAppendingPathComponent:sound.lastPathComponent] fromSource:sound];
    }
    
//    NSError *error;
//    只返回下一层的子目录
//    NSArray<NSString *> *contents = [fm contentsOfDirectoryAtPath:docPath error:&error];
//    NSLog(@"%@",contents);
//    for (NSString *content in contents) {
//        NSString *tmpContent = [docPath stringByAppendingPathComponent:contents];
//        BOOL isDir;
//        // 存在且是文件夹
//        if ([fm fileExistsAtPath:tmpContent isDirectory:&isDir] && isDir) {
//            // 移动
//        }
//    }
    
    NSLog(@"1");
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)coverDes:(NSString *)des fromSource:(NSString *)source {
    NSFileManager *fm = __fm();
    NSError *error;
    if ([fm fileExistsAtPath:des]) {
        NSString *des1 = [NSString stringWithFormat:@"%@1", des];
        [fm moveItemAtPath:des toPath:des1 error:&error];
        if (!error) {
            [fm copyItemAtPath:source toPath:des error:&error];
            if (!error) {
                [fm removeItemAtPath:des1 error:&error];
            }
        }
    }
    else {
        [fm copyItemAtPath:source toPath:des error:&error];
    }
    NSLog(@"%@", error);
}


@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值