Objective c -enumaeratorAtPath: Not Working at root level folders - objective-c

I am trying to enumerate files and folders at root level
(/Users/Myname/Desktop or /Users/Myname/Documents),[enumarator nextobject] always gives nil value.
How can i solve this issue.
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:#"/Users/Myname/Desktop"];
NSString *content= [enumerator nextObject];//content always nil.

Try this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator * emuerator = [fileManager enumeratorAtPath:#"/usr/Myname/Desktop"];
NSString *filename; while ((filename = [emuerator
nextObject]))
{
NSLog (#"file! %#", filename);
}
OR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSFileManager *manager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *emuerator= [manager enumeratorAtPath: #"/usr/Myname/Desktop"];
for (NSString *filename in emuerator) {
// Do something with file
}
[manager release];

Related

Plist/Document Directory is null when reading plist

I just want to replace static plist data to dynamic.this is my code
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"sample.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
i have some data in event array but its not writing/reading can you please suggest me where i am wrong.
NSLog(#"data coming from server....%#",self.eventDataArray);
//Writing array to document directory
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [docpaths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/sample.plist", documentsDirectoryPath];
[self.eventDataArray writeToFile:path atomically:YES];
NSLog(#"plist path string....%#",path);
//reading from document directory
NSMutableArray *readingArray=[[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"plist reading data is here......%#",readingArray);

Saving NSMutableDictionary to plist iOS

This has become very frustrating over the past day-and-a-bit-too-much: why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
I have used it before, and it worked perfectly: is there something I am forgetting to add in my info.plist? That is the only thing I can think of.
Thank you for helping!
EDIT:
Maybe it has something to do with Xcode 4.5 and iOS6? It worked in 4.3/iOS5
Have you added the .plist file to your project? If so, try this:
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
If not, create the property list in your project.

Delete empty folders in iOS?

I have plenty of empty temporary folders in app's Document folder.
How to delete them all?
I tried:
NSArray *folders = [[NSFileManager defaultManager]contentsOfDirectoryAtURL:[self applicationDocumentsDirectory] includingPropertiesForKeys:[NSArray arrayWithObject:#"NSURLIsDirectoryKey"] options:0 error:nil];
if (folders) {
for (NSURL *url in folders) {
[[NSFileManager defaultManager]removeItemAtURL:url error:nil];
}
}
but it deletes all, not only folders
This snippet of code deletes only directiories that are empty.
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *file in files) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];
NSError *error;
if ([[fileManager contentsOfDirectoryAtPath:fullPath error:&error] count]==0) {
// check if number of files == 0 ==> empty directory
if (!error) {
// check if error set (fullPath is not a directory and we should leave it alone)
[fileManager removeItemAtPath:fullPath error:nil];
}
}
}
If you simply want to delete all directories (both empty and not empty) the snippet can be simplified into this:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *file in files) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];
if ([fileManager contentsOfDirectoryAtPath:fullPath error:nil])
[fileManager removeItemAtPath:fullPath error:nil];
}

How to fill an NSArray with directory names?

I'm looking for a way to fill an NSArray with NSStrings that are the names of the directories in the apps documents directory.
This is what I have so far:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (//loop until no more directories)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:path];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
I'm not sure what the for loop's conditions need to be and I'm not sure how to retrieve the directories names.
Thanks for the advice in advance.
Try this
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:urDocumentsFolderPath error:nil] retain];
for(NSString *file in filesArray) {
//Do something
}
So in your case
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil] retain];
for(NSString *file in filesArray)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:file];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
Hope this helps
"apps documents directory" = ~/Users/User/Applications? NSDocumentsDirectory or NSApplicationDirectory?
Maybe:
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileIsDirectory = FALSE;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *content = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; //OSX v10.5
NSEnumerator *enumeratorContent = [content objectEnumerator];
NSString *file;
while ((file = [emumeratorContent nextObject])) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
if (fileManager fileExistAtPath:[documentsDirectory stringByAppendingPathComponent:file] isDirectory:&fileIsDirectory && fileIsDirectory)
[directoriesArray addObject:file];
[pool drain];
Try to use NSDirectoryEnumerator(OSX v.10.0) or NSURL too (OSX v.10.6).

I have successfully saved my plist to the documents directory! Now I can't load it back

This code works beautifully to save my plist to the documents directory.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
I have not been able to figure out how to load the plist back to the app!
Can anyone help? Thanks.
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];
didn't work?
If you are asking how to overwrite a plist included in your app's main bundle, then you should know that it cannot be done. Take a look at this question for more details: Can you update a file in the application bundle?
NSError *error; NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
docDirPath = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
}
NSMutableArray *tmpArray = [[[NSMutableArray alloc] initWithContentsOfFile: docDirPath] mutableCopy];