0

In my app I want to copy files from one directory to another directory. In the source directory there are files and folders.

I do this in the code below:

- (BOOL)copyDirectory:(NSString*)Directory toDirectory:(NSString*)targetDirectory
{
    NSLog(@"start copy");
    @try
    {
        NSError *error = nil;
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
        NSString *folderPath = [path stringByAppendingPathComponent:targetDirectory];
        BOOL succes = [[NSFileManager defaultManager] fileExistsAtPath:folderPath];
        if(succes){
            return NO;
        }
        else
        {
            NSString *wwwPath = [[NSBundle mainBundle] pathForResource:Directory ofType:nil];
            NSArray *target= [[NSFileManager defaultManager] contentsOfDirectoryAtPath:wwwPath error:&error];

            [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:nil];
            int a = 0 ;
            for(a = 0 ; a < target.count;a++){
                NSString *temp = [target objectAtIndex:a];
                if([temp rangeOfString:@"."].location == NSNotFound)
                {
                    NSString *sourceDirectory = [NSString stringWithFormat:@"%@/%@",Directory,temp ];
                    NSString *DestanistionDirectory = [NSString stringWithFormat:@"%@/%@",targetDirectory,temp];

                    if([self copyDirectory:sourceDirectory toDirectory:DestanistionDirectory] == NO){
                        return NO;
                    }
                }
                else
                {
                    NSString *source = [NSString stringWithFormat:@"%@/%@",wwwPath,temp];
                    NSString *target = [NSString stringWithFormat:@"%@/%@",folderPath,temp];

                    BOOL result = [[NSFileManager defaultManager] copyItemAtPath:source toPath:target error:&error];
                    if(result)
                        currentFileCounter++;
                    [self performSelectorInBackground:@selector(updatePrograsBar) withObject:nil];
                    if(error != nil){
                        if(IsDEBUG) NSLog(@"error: %@",[error description]);
                        return NO;
                    }
                }
            }
        }
    }
    @catch (NSException *exception)
    {
        if(IsDEBUG)NSLog(@"%@",exception.description);
        return NO;
    }
    NSLog(@"end copy");
    return YES;
}

To save time, when I call to

- (BOOL)copyDirectory:(NSString*)Directory to Directory:(NSString*)target Directory 

in recursive I call in new thread.

- (BOOL)copyDirectory:(NSString*)Directory toDirectory:(NSString*)targetDirectory
{
    NSLog(@"start copy");
    @try
    {
        NSError *error = nil;
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
        NSString *folderPath = [path stringByAppendingPathComponent:targetDirectory];
        BOOL succes = [[NSFileManager defaultManager] fileExistsAtPath:folderPath];
        if(succes){
            return NO;
        }
        else
        {
            NSString *wwwPath = [[NSBundle mainBundle] pathForResource:Directory ofType:nil];
            NSArray *target= [[NSFileManager defaultManager] contentsOfDirectoryAtPath:wwwPath error:&error];

            [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:nil];
            int a = 0 ;
            for(a = 0 ; a < target.count;a++){
                NSString *temp = [target objectAtIndex:a];
                if([temp rangeOfString:@"."].location == NSNotFound)
                {
                    NSString *sourceDirectory = [NSString stringWithFormat:@"%@/%@",Directory,temp ];
                    NSString *DestanistionDirectory = [NSString stringWithFormat:@"%@/%@",targetDirectory,temp];

                    dispatch_async(dispatch_get_global_queue(0, 0), ^{


                        [self copyDirectory:sourceDirectory toDirectory:DestanistionDirectory];

                    });                   
                }
                else
                {
                    NSString *source = [NSString stringWithFormat:@"%@/%@",wwwPath,temp];
                    NSString *target = [NSString stringWithFormat:@"%@/%@",folderPath,temp];

                    BOOL result = [[NSFileManager defaultManager] copyItemAtPath:source toPath:target error:&error];
                    if(result)
                        currentFileCounter++;
                    [self performSelectorInBackground:@selector(updatePrograsBar) withObject:nil];
                    if(error != nil){
                        if(IsDEBUG) NSLog(@"error: %@",[error description]);
                        return NO;
                    }
                }
            }
        }
    }
    @catch (NSException *exception)
    {
        if(IsDEBUG)NSLog(@"%@",exception.description);
        return NO;
    }
    NSLog(@"end copy");
    return YES;
}

but I need to know when is finish to copy all the files and folder and not return from the main call to

-(BOOL)copyDirectory:(NSString*)Directory to Directory:(NSString*)target Directory

until finish all.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41

0 Answers0