Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Appending Date to a file's name when uploaded

SevenOTwoSevenOTwo Posts: 11Registered Users
Hello All,

I am wanting to append the Date and Time to the sqlite file name I am uploading to dropbox. what is the correct way to do this? below is the code I have thus far for uploading to the dropbox service, it works perfectly. I am also wanting to be able to strip the date and time stamp from the newest file (The date which is closest to the now() date, so the most recent backup can be retrieved. any help is appreciated.

Thanks,

James

Here's the code:



#pragma mark -
#pragma mark Core Data stack


//////////////////////////////////////////////////////////////////
// Returns the managed object context for the application. //
// If the context doesn't already exist, it is created and //
// bound to the persistent store coordinator for the //
// application. //
//////////////////////////////////////////////////////////////////


- (NSManagedObjectContext *) managedObjectContext {

if (managedObjectContext != nil) {

return managedObjectContext;

}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

if (coordinator != nil) {

managedObjectContext = [NSManagedObjectContext new];

[managedObjectContext setPersistentStoreCoordinator: coordinator];

}

return managedObjectContext;
}


//////////////////////////////////////////////////////////////////
// Returns the managed object model for the application. //
// If the model doesn't already exist, it is created by //
// merging all of the models found in the application bundle. //
//////////////////////////////////////////////////////////////////


- (NSManagedObjectModel *) managedObjectModel {

if (managedObjectModel != nil) {

return managedObjectModel;

}

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles: nil] retain];

return managedObjectModel;

}


//////////////////////////////////////////////////////////////////
// Returns the persistent store coordinator for the application.//
// If the coordinator doesn't already exist, it is created and //
// the application's store added to it. //
//////////////////////////////////////////////////////////////////


- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {

return persistentStoreCoordinator;

}

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kPersistentStore];


//////////////////////////////////////////////////////////////////
// Set up the store. //
// For the sake of illustration, provide a pre-populated //
// default store. //
//////////////////////////////////////////////////////////////////


NSFileManager *fileManager = [NSFileManager defaultManager];


//////////////////////////////////////////////////////////////////
// If the expected store doesn't exist, copy the default store. //
//////////////////////////////////////////////////////////////////


if (![fileManager fileExistsAtPath:storePath]) {

NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource: @"/database" ofType: @"sqlite"];

if (defaultStorePath) {

[fileManager copyItemAtPath: defaultStorePath toPath: storePath error: NULL];

}

}

NSURL *storeUrl = [NSURL fileURLWithPath: storePath];

NSError *error;

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
configuration: nil
URL: storeUrl
options: nil
error: &error]) {


//////////////////////////////////////////////////////////////////
// //
// Replace this implementation with code to handle the error //
// appropriately. abort() causes the application to generate a //
// crash log and terminate. You should not use this function in //
// a shipping application, although it may be useful during //
// development. If it is not possible to recover from the error,//
// display an alert panel that instructs the user to quit the //
// application by pressing the Home button. //
// //
// Typical reasons for an error here include: //
// ***> The persistent store is not accessible //
// ***> The schema for the persistent store is incompatible with//
// current managed object model. //
// //
// Check the error message to determine what the //
// actual problem was. //
// //
//////////////////////////////////////////////////////////////////


NSLog (@"Unresolved error %@, %@", error, [error userInfo]);

abort ();

}

return persistentStoreCoordinator;

}

#pragma mark -
#pragma mark Download the File from DropBox Cloud


//////////////////////////////////////////////////////////////////
// DOWNLOAD FROM DROPBOX: //
//////////////////////////////////////////////////////////////////


- (IBAction) downloadFromDropBox {

documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

documentsDirectory = [documentPaths objectAtIndex:0];

[self.restClient loadFile: [NSString stringWithFormat: @"%@/%@", dropBoxFolder, dbFileName]
intoPath: [NSTemporaryDirectory () stringByAppendingPathComponent: dbFileName]];

UIAlertView *error1 = [[UIAlertView alloc] initWithTitle: @"Your database has been downloaded from DropBox"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];

[error1 show];

[error1 release];


}

- (void) restClient: (DBRestClient*) client loadedFile: (NSString*) destPath contentType: (NSString*) contentType {

[(DoctorsAppDelegate *) [UIApplication sharedApplication].delegate openPersitentStoreAtPath: destPath];

}

- (void) restClient: (DBRestClient*) client loadFileFailedWithError: (NSError*)error {

UIAlertView *errorView = [[UIAlertView alloc] initWithTitle: @"Download Failed"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];

[errorView show];

[errorView release];

}

#pragma mark -
#pragma mark Upload the File to DropBox


//////////////////////////////////////////////////////////////////
// UPLOAD TO DROPBOX: //
//////////////////////////////////////////////////////////////////


- (IBAction) uploadToDropBox {

[self.restClient uploadFile: dbFileName
toPath: dropBoxFolder
fromPath: [[self applicationDocumentsDirectory]stringByAppendingPathComponent: dbFileName]];

[self.restClient uploadFile: dbFileName
toPath: dropBoxFolderHidden
fromPath: [[self applicationDocumentsDirectory]stringByAppendingPathComponent: dbFileName]];

UIAlertView *error2 = [[UIAlertView alloc] initWithTitle: @"Your database has been uploaded to DropBox"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];

[error2 show];

[error2 release];

}


#pragma mark -
#pragma mark Application's documents directory


//////////////////////////////////////////////////////////////////
// RETURNS THE PATH TO THE APPLICATIONS DOCUMENTS DIRECTORY: //
//////////////////////////////////////////////////////////////////


- (NSString *) applicationDocumentsDirectory {

return [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

}

- (void) dealloc {

[managedObjectContext release];

[managedObjectModel release];

[persistentStoreCoordinator release];

[link release];

[download release];

[upload release];

[super dealloc];

}

@end
Post edited by SevenOTwo on

Replies

  • dany_devdany_dev Posts: 4,700Tutorial Authors, Registered Users

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@\"dd-MM-yyyy\"];

    NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];

    [formatter release];

    NSString *fileName = [NSString stringWithFormat:@\"Database-%@.sqlite\",stringFromDate];
    NSString *newPath= [[NSHomeDirectory() stringByAppendingPathComponent:@\"Documents\"] stringByAppendingPathComponent:fileName];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError * err = NULL;

    BOOL result = [fileManager moveItemAtPath:dbPath toPath:newPath error:&err];
  • SevenOTwoSevenOTwo Posts: 11Registered Users
    Thank You for the Quick response, where would the best place to put this snippet? in the App delegate data Stack? or in the uplpoad to DropBox method?

    Any suggestions on how toselect the file on the server with the date closest to the Now() date? and remove the appended date string??

    Thanks You for your assistance,

    James
    dany_dev;330638 said:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@\"dd-MM-yyyy\"];

    NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];

    [formatter release];

    NSString *fileName = [NSString stringWithFormat:@\"Database-%@.sqlite\",stringFromDate];
    NSString *newPath= [[NSHomeDirectory() stringByAppendingPathComponent:@\"Documents\"] stringByAppendingPathComponent:fileName];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError * err = NULL;

    BOOL result = [fileManager moveItemAtPath:dbPath toPath:newPath error:&err];
  • dany_devdany_dev Posts: 4,700Tutorial Authors, Registered Users
    my code take the file in path "dbPath" and rename it on "Database-Date.sqlite"

    So put where you want before send it, just remember the new path to upload it to dropbox. I not read your code, because you haven't use tag
     to show it.


    [CODE]Any suggestions on how toselect the file on the server with the date closest to the Now() date? and remove the appended date string??


    an information like that should be not on file name, I would use last edit date directly from dropbox, as you can see in documentation you can retrieve it.
    However you can do also as you said, once that you have an array of file, just take the file that start with Database- and then just scrape the data, and convert to NSData so that you will have an array of NSData, compare to retrieve the most recent and then download the file renaming it.

    I will not write the code for you.
  • SevenOTwoSevenOTwo Posts: 11Registered Users
Sign In or Register to comment.