Im trying to create an folder in user library directory,but its not creating. Is there any mistake in the below code.
databaseName = #"KITSMAW0051_DB.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folder = #"Swasstik";
databasePath = [[documentsDir stringByAppendingPathComponent:folder ] stringByAppendingPathComponent:databaseName];
Ok first of all databasePath describes a path to a file not a directory. But let's say you actually want to create "Swasstik" folder inside user's Library:
// Your code
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folder = #"Swasstik";
// I edited here to keep only the folder
NSString *folderPath = [documentsDir stringByAppendingPathComponent:folder];
// At this point we got the path to the folder
// but we want to actually go on and create it
// NSFileManager to the rescue!
NSFileManager *manager = [NSFileManager defaultManager];
[manager createDirectoryAtPath: folderPath
withIntermediateDirectories: NO
attributes: nil
error: nil];
Of course you can study and set the manager as you like and also implement some error handling. I hope that this makes sense...
databaseName = #"KITSMAW0051_DB.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folder = [documentsDir stringByAppendingPathComponent:#"Swasstik"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if (![fileManager fileExistsAtPath:folder]) {
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:&error];
}
if (error != nil) {
NSLog(#"Some error: %#", error);
return;
}
NSString *databasePath = [folder stringByAppendingPathComponent:databaseName];
Related
I have an app that I want to save a username for each person that I play a game with and then retrieve it into an array that can be used to create headers for a tableview. I am using this code to save the files.
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *slash2 = [documentsDirectory stringByAppendingString:#"/Names"];
if (![[NSFileManager defaultManager] fileExistsAtPath:slash2])
[[NSFileManager defaultManager] createDirectoryAtPath:slash2 withIntermediateDirectories:NO attributes:nil error:&error1];
//yourName is the userName of the challenger
NSString *fullPath = [slash2 stringByAppendingPathComponent:yourName];
and when I look at the path
NSLog(#"path :%#",fullPath);
I get
"Names/Mary",
"Names/John",
I would like to then get these names form the documents directory into an array to use is the tableview. I have tried
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *slasha = [documentsDirectory stringByAppendingPathComponent:#"/Names"];
NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:slasha];
for (NSString *element in arrayFromFile)
NSLog(#"Names: %#", element);
but nothing shows up
What am I doing wrong and how can I do this
user1114881,
Creating folders to show challengers name in tableView header, Seriously ??? I will say its a very wrong approach.
That being said, now let me fix your problem,
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *slash2 = [documentsDirectory stringByAppendingString:#"/Names"];
if (![[NSFileManager defaultManager] fileExistsAtPath:slash2])
[[NSFileManager defaultManager] createDirectoryAtPath:slash2 withIntermediateDirectories:NO attributes:nil error:&error1];
//yourName is the userName of the challenger
NSString *fullPath = [slash2 stringByAppendingPathComponent:yourName];
BOOL Success = [[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil];
Mistake you were doing was, you never created a file for the user, you just created a folder called Names. You need to call createFileAtPath to create file.
In order to read all the files in folder use
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *slasha = [documentsDirectory stringByAppendingPathComponent:#"/Names"];
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:slasha error:nil];
You must be able to see the list of all files inside folder Names now :)
Happy coding.
I need to create a folder and files in this path
/var/mobile/Containers/Data/
but when I look for it can't find the folder
my code >>
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = #"/var/mobile/Containers";//[paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"MyNewFolder/myDB.db"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
I dint really understood your problem . Hope this helps :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(#"Saving");
NSUserDefaults *prefs = [[NSUserDefaults alloc] init];
NSString *path1 = [NSString stringWithFormat:#"/var/mobile/Containers"];
NSString *dataPath = [path stringByAppendingPathComponent:path1];
dataPath = [dataPath stringByStandardizingPath];
filePath = [dataPath stringByAppendingPathComponent:#"MyNewFolder/myDB.db"];
Here's the iOS 8 approach Apple recommends to retrieve the location of the Documents directory:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Create Folder
//-- to select path from image--//
AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *dataPath=[del.databasePath copy];
NSString *Documentpath=[del.path_Folder_Document copy];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:#"/Images"];
Pathforimage=dataPath;
Add Folder if not Exist
if (![[NSFileManager defaultManager] createDirectoryAtPath:Pathforimage withIntermediateDirectories:NO attributes:nil error:nil])
{
//NSLog(#"directory... /Images already exist..");
}
else
{
//NSLog(#"directory created!");
}
NSString *pathforAudios=[NSString stringWithFormat:#"%#/Images_Audio",Documentpath];
if (![[NSFileManager defaultManager] createDirectoryAtPath:pathforAudios withIntermediateDirectories:NO attributes:nil error:nil])
{
//NSLog(#"directory... /Images already exist..");
}
else
{
//NSLog(#"directory created!");
}
//-- end of path --//
Save Image to Folder
//Save Image to Documents Directory
NSString *imagePath = [Pathforimage stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg",Img_File_Name]];
[UIImageJPEGRepresentation(image, 0.1) writeToFile:imagePath atomically:YES];
imagePath=nil;
image=nil;
Delete Files/Folder
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files =%#",files);
NSString *Images_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:#"/Images"];
NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:Images_Folder error:nil];
for (NSString *filename in fileArray)
{
NSLog(#"Images =%#",filename);
[fileMgr removeItemAtPath:[Images_Folder stringByAppendingPathComponent:filename] error:NULL];
}
NSString *Images_Audio_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:#"/Images_Audio"];
NSArray *fileArray1 = [fileMgr contentsOfDirectoryAtPath:Images_Audio_Folder error:nil];
for (NSString *filename in fileArray1)
{
NSLog(#"Images_Audio =%#",filename);
[fileMgr removeItemAtPath:[Images_Audio_Folder stringByAppendingPathComponent:filename] error:NULL];
}
Create floder
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyNewFolder"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dataPath]){
[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder
}else{
NSLog(#"exist");
}
NSLog(#"dataPath:%#",dataPath);
I want to create a file in my application document folder and i have to store some content. That content takes from my localhost file. So i write a below code. But this code didn't create a file in my application document folder.
- (void) createFile
{
NSString *strPath = [NSString stringWithFormat:#"http://192.168.5.117/~mac/banana.obj"];
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];
NSString *strLastpath = [strPath lastPathComponent];
NSString *folderName = [#"Object File/" stringByAppendingString:strLastpath];
NSLog(#"Path : %# \n File Name : %#",strPath,folderName);
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:folderName]];
NSLog(#"Database Path : %#",databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:databasePath] == YES)
{
NSLog(#"File Exists");
}
else
{
NSLog(#"File not exists");
}
NSString *content = [NSString stringWithFormat:#"%#",data];
[content writeToFile:strLastpath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}
While build for running i got this.
2013-10-11 11:36:38.833 SampleFileCreation[1321:c07] Path : http://192.168.5.117/~mac/banana.obj
File Name : Object File/banana.obj
2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] Database Path : /Users/mac/Library/Application Support/iPhone Simulator/6.1/Applications/4FA749DF-D12D-4956-AF76-140D2F981F17/Documents/Object File/banana.obj
2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] File not exists
You can also try this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *fileName = #"yourFileName";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/fileName"];
NSDate *data = [NSData dataWithContentsOfURL:YourUrl]; // get date form url
[data writeToFile:dataPath atomically:YES]; // save data in file
I found the answer. File created successfully.
- (void) createFile {
NSString *strPath = [NSString stringWithFormat:#"http://-.-.-.-/~mac/banana.obj"];
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];
NSString *strLastpath = [strPath lastPathComponent];
NSString *folderName = [#"Object File/" stringByAppendingString:strLastpath];
NSLog(#"Path : %# \n File Name : %#",strPath,folderName);
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
docsDir=[docsDir stringByAppendingString:#"/Object File"];
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent:folderName]];
NSLog(#"Database Path : %#",databasePath);
NSError *theError = nil;
NSFileManager *filemgr = [NSFileManager defaultManager];
**//I added this line**
[filemgr createDirectoryAtPath:docsDir
withIntermediateDirectories:YES
attributes:nil
error:&theError];
**//Changed this line**
[data writeToFile:[docsDir stringByAppendingPathComponent:strLastpath]
options:NSDataWritingAtomic
error:&theError];
if ([filemgr fileExistsAtPath:[docsDir stringByAppendingPathComponent:strLastpath]]){
NSLog(#"File Exists");
} else {
NSLog(#"File not exists");
NSLog(#"Tell Me error %#",[theError localizedDescription]);
}
}
Hello i am trying to copy text file from document directory into folder name Temp that located in document directory.
Here is some of code i tried and didn't work.
self.fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsDirectoryToTemp = [NSString stringWithFormat:#"%#/Temp/",[paths objectAtIndex:0]];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.txt",tblGetNameToMove.textLabel.text]];
[self.fileManager copyItemAtPath:txtPath toPath:documentsDirectoryToTemp error:&error];
How can i do that?
There could be many reasons for this :
First try to print all the path like documentsDirectoryToTempn and txtPath whether it is correct or not .
The second thing is make use that error variable :
if(error)
{
NSLog("Error while coping :%#",[error localizedDiscription]);
}
so you can track exact problem .
Here is solution. I need to add directory and also name of file and extension.
Here is codes.
self.fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.txt",tblGetNameToMove.textLabel.text]];
NSString *fileName = [NSString stringWithFormat:#"%#/Temp/%#.txt",documentsDirectory,tblGetNameToMove.textLabel.text];
[self.fileManager copyItemAtPath:txtPath toPath:fileName error:&error];
I use sqlite and fmdb wrapper. My code for load db is this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:#"biscuit.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
I add biscuit.sqlite to my project, but this code create new file instead open existing one. How can I load this file?
If you only want to read data from your database, try to use the code below:
NSString *path = [[NSBundle mainBundle] pathForResource:#"biscuit" ofType:#"sqlite"];
If you need read/write access, you have to copy your database to a writable user folder first.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:#"biscuit.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"biscuit.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success) {
NSLog(#"Failed to create writable DB. Error '%#'.", [error localizedDescription]);
} else {
NSLog(#"DB copied.");
}
}else {
NSLog(#"DB exists, no need to copy.");
}