DannyK
DannyK

Reputation: 47

How to add timestamp to my filePath?

I have a static file path to a download folder, files download fine but when I download a new one the old one gets replaced by new. I suppose because of static pathing. How do I add a timestamp to my code so whenever a new download is made the old one wouldn't get replaced?Or maybe even give download file the "original" filename from the actual array?

here is my download code:

-(void) Savefile {

    [self.downloadfile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error)  {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wooopss!" message:@"Download failed. Try Again..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else if (data) {


            [data writeToFile:@"/Users/Danny/Desktop/PDFFile/hello.pdf" atomically:NO ];



            NSLog(@"Downloading file...");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Downloading" message:@"File is being downloaded..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];

}

Upvotes: 0

Views: 767

Answers (1)

random
random

Reputation: 8608

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MMddYYYYmmss"];

NSString *filePath = [NSString stringWithFormat:@"/Users/Danny/Desktop/PDFFile/hello_%@.pdf", [dateFormatter stringFromDate:[NSDate date]]];

Which will give you this:

/Users/Danny/Desktop/PDFFile/hello_032620144401.pdf

Not entirely sure what you mean by:

Or maybe even give download file the "original" filename from the actual array

Upvotes: 1

Related Questions