Created
March 7, 2012 21:13
-
-
Save tudormunteanu/1996287 to your computer and use it in GitHub Desktop.
FileManagerHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // FileManagerHelper.h | |
| #import <Foundation/Foundation.h> | |
| @interface FileManagerHelper : NSObject | |
| + (void) cleanDocumentsDir; | |
| + (void) changeCapitalJPGNames; | |
| // | |
| // Return the path to the Documents directory | |
| // | |
| + (NSString *) documents; | |
| // | |
| // Delete all the files in Documents | |
| // | |
| + (void) eraseAllDocuments; | |
| // | |
| // | |
| // | |
| + (BOOL) createDirectoryAtPath:(NSString *)path; | |
| // | |
| // | |
| // | |
| + (BOOL) fileExistsAtPath:(NSString *)path; | |
| @end | |
| /*******************************************************/ | |
| // FileManagerHelper.m | |
| #import "FileManagerHelper.h" | |
| @implementation FileManagerHelper | |
| + (void) cleanDocumentsDir { | |
| // Disable this temporary | |
| // return; | |
| NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| int count; | |
| NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:NULL]; | |
| for (count = 0; count < (int)[directoryContent count]; count++) { | |
| NSString *file = [directoryContent objectAtIndex:count]; | |
| NSString *pathToDelete = [[paths objectAtIndex:0] stringByAppendingPathComponent:file]; | |
| NSError *error; | |
| if ([[NSFileManager defaultManager] fileExistsAtPath:pathToDelete]) { | |
| if (![[NSFileManager defaultManager] removeItemAtPath:pathToDelete error:&error]) { | |
| NSLog(@"Delete file error: %@", error); | |
| } | |
| } | |
| } | |
| } | |
| + (void) changeCapitalJPGNames { | |
| NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
| NSString *path = [documents stringByAppendingPathComponent:@"pimexport"]; | |
| NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL]; | |
| NSError *error; | |
| for (NSString *filename in files) { | |
| NSString *ext = [filename pathExtension]; | |
| // NSLog(@"ext found %@", ext); | |
| if ([[ext lowercaseString] isEqualToString:@"jpg"]) { | |
| NSString *newExt = [ext lowercaseString]; | |
| NSString *newFilename = [NSString stringWithFormat:@"%@.%@", [filename stringByDeletingPathExtension], newExt]; | |
| NSString *src = [path stringByAppendingPathComponent:filename]; | |
| NSString *dest = [path stringByAppendingPathComponent:newFilename]; | |
| NSString *tmpName = [dest stringByAppendingString:@"tmp"]; | |
| // NSLog(@"trying to rename file %@ to %@", filename, newFilename); | |
| if (![[NSFileManager defaultManager] moveItemAtPath:src toPath:tmpName error:&error]) { | |
| // NSLog(@"error renaming to tmp file %@, %@", filename, error); | |
| } | |
| if (![[NSFileManager defaultManager] moveItemAtPath:tmpName toPath:dest error:&error]) { | |
| // NSLog(@"error renaming from file %@, %@", filename, error); | |
| } | |
| } | |
| } | |
| } | |
| + (NSString *) documents { | |
| return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
| } | |
| + (void) eraseAllDocuments { | |
| NSFileManager *fm = [NSFileManager defaultManager]; | |
| NSString *directory = [FileManagerHelper documents]; | |
| NSError *error = nil; | |
| for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) { | |
| BOOL success = [fm removeItemAtPath:[directory stringByAppendingPathComponent:file] error:&error]; | |
| if (!success || error) { | |
| // it failed. | |
| } | |
| } | |
| } | |
| + (BOOL) createDirectoryAtPath:(NSString *)path { | |
| BOOL isDir = YES; | |
| if(![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) { | |
| if(![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]) { | |
| return NO; | |
| } | |
| } | |
| return YES; | |
| } | |
| + (BOOL) fileExistsAtPath:(NSString *)path { | |
| BOOL isDir = YES; | |
| if(![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) { | |
| return NO; | |
| } | |
| return YES; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment