Question

I want to use progress bar to show the progress while I am copying a file from one place to another. So here is the code for copying file:

 if([fileManager isReadableFileAtPath:sourcePath])
    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:nil];

And here is the code for progressing bar (I use NSProgressIndicator):

// Set the max value to our source file size
[_localProgressIndicator setMaxValue:fileSizeTotal];
[_localProgressIndicator setDoubleValue:0.0];

//Start showing progress bar in using NSTime
if(!timerForProgressBar){
    timerForProgressBar = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                            target:self
                                                         selector:@selector(checkCopyingPorgress:)
                                                          userInfo:nil
                                                           repeats:YES];
    [_localProgressIndicator startAnimation:self];
}

-(void)checkCopyingPorgress:(NSTimer *)aTimer
{
  if(fileCurrentSize >= fileSizeTotal)
  {
     fileCurrentSize = 0;
     [aTimer invalidate];
     aTimer = NULL;
     [_localProgressIndicator setDoubleValue:0.0];
     [_localProgressIndicator stopAnimation: self];
  }
  else
  {
    [_localProgressIndicator setDoubleValue: fileCurrentSize/100.0];
    [_localProgressIndicator displayIfNeeded];
  }
}

So my question is how to get the "fileCurrentSize" from fileManager when I am copying a file from one place to another?? Thanks !!

Was it helpful?

Solution

You may use

- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error

and then fetch [dictObject valueForKey@"NSFileSize"]; from the file property dictionary

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top