Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. /**
  2. Returns the path to the application's Documents directory.
  3. */
  4. - (NSString *)applicationDocumentsDirectory {
  5. return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  6. }
  7.  
  8. #import "StringUtils.h"
  9.  
  10. @implementation StringUtils
  11.  
  12. + (NSString *)getFullDocumentUrl:(NSString *)fileName
  13. {
  14. return [NSString stringWithFormat:@"%@/%@",[self applicationDocumentsDirectory],fileName];
  15. }
  16.  
  17. + (NSString *)applicationDocumentsDirectory
  18. {
  19. return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  20. }
  21.  
  22. @end
  23.  
  24. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  25. NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
  26. NSFileManager *fm = [NSFileManager defaultManager];
  27. NSArray *filenames = [fm contentsOfDirectoryAtPath:documentsPath error:nil];
  28.  
  29. //Match on the filepath leading up to docs directory
  30. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^/\S*Documents/"
  31. options:NSRegularExpressionCaseInsensitive
  32. error:nil];
  33. for (NSString *fileName in filenames)
  34. {
  35. NSRange fullLength = NSMakeRange(0, [fileName length]);
  36. //swap out the prepended directory structure with an empty string
  37. NSString *updatedFileName = [regex stringByReplacingMatchesInString:fileName
  38. options:0
  39. range:fullLength
  40. withTemplate:@""];
  41. NSString *currentName = [NSString stringWithFormat:@"%@/%@",documentsPath,fileName];
  42. NSString *updatedName = [NSString stringWithFormat:@"%@/%@",documentsPath,updatedFileName];
  43. [fm moveItemAtPath:currentName toPath:updatedName error:nil];
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement