I want to create a directory inside my applicationSupportDirectory. My understanding is that the applicationSupportDirectory does not allow users to see the data within. This is why I have chosen it. However, the code I am using below seems to fail and I am not sure why.
Can anyone tell me what I have done wrong? Thanks!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0]; // Get directory
NSString *dataPath = [applicationDirectory stringByAppendingPathComponent:#"drawings"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
;// success
else
{
NSLog(#"Failed");
}
}
}
Try this one, it works for me.
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)[0];
NSString *folder = [documentsPath stringByAppendingPathComponent:#"foldername"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if (![fileManager fileExistsAtPath:folder]){
[fileManager createDirectoryAtPath:folder
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
NSString *directoryName = #"drawing";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [applicationDirectory stringByAppendingPathComponent:directoryName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
Hope it will help you
To create Dirtectory
NSError *error;
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/Application Support/directoryname"];
NSLog(#"%#",path);
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
// Set do not backup attribute to whole folder
}
Related
I have a sqlite file that I am trying to store in Application Support Folder. I am using the following code to create the directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSError *err;
NSString *dataPath = [applicationSupportDirectory stringByAppendingPathComponent:#"/Email"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&err]; //Create folder
NSString *dataBasePath = [applicationSupportDirectory stringByAppendingString:#"/Email/Email.sqlite"];
This is the printed result when I NSLog dataBasePath
/var/mobile/Containers/Data/Application/8A40E5A9-EEE6-46EF-B71F-36C9A00D5996/Library/Application
Support/Email/Email.sqlite
Is this correct way of creating the directory to store my sql file in Application Support? I have to use Application Support so the user can't mess with it (I use file share so document directory is out of the question) and the OS shouldn't be able to mess with it either. I just need to be sure
It's close but not quite right. Don't put the / before Email. You want to pass YES to withIntermediateDirectories. And the final path should use dataPath and stringByAppendingPathComponent.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSString *dataPath = [applicationSupportDirectory stringByAppendingPathComponent:#"Email"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
NSError *err;
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&err]) { //Create folder
NSLog(#"Unable to create folder at %#: %#", dataPath, err);
}
}
NSString *dataBasePath = [dataPath stringByAppendingPathComponent:#"Email.sqlite"];
I want remove specific item from DocumentDirectory i.e I have 50 objects in the DocumentDirectory and i want to remove the 5 different elements from it. I used following code to get the DocumentDirectory objects.
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files array %#", filePathsArray);
can any one Help me out to solve this problem
You can try the following approach
NSString *path = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [paths[0] stringByAppendingPathComponent:#"YOUR DIR"];
path = [path stringByAppendingPathComponent:#"YOUR FILE"];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) //Delete it
{
NSLog(#"Delete file error: %#", error);
}
}
You can try like this way
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (filePathsArray.count >= 6) {
for (NSInteger i=filePathsArray.count-6; i < filePathsArray.count; i++) {
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:i]];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSError *error;
if (![[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]) {
NSLog(#"Delete error: %#", error);
}
}
}
}
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);
im seeking for assistant.In my case i have file such as
RN150622103444544_pr.pdf
RN150622103444544_ID_GD.pdf
RN150622103444544_CA.xml
My question is how can i delete all the file only by referring the RN150622103444544.
Currently my code is deleting one by one.
Below are my sample code
-(void)deleteOldPdfs:(NSString *)proposal
{
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_pr_1.pdf",proposal]]])
{
[fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_pr_1.pdf",proposal]] error:&error];
}
if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_id_GD.pdf", proposal]]]){
[fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_id_GD.pdf", proposal]] error:&error];
}
if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_CA.xml", proposal]]]){
[fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_CA.xml",proposal]] error:&error];
}
}
Thank in advance
You could put the unique suffixes in an array and then loop over the array.
Sample code (untested):
NSArray *suffixList = #[#"pr_1.pdf", #"id_GD.pdf", #"CA.xml"];
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
for (NSString *suffix in suffixList) {
NSString *path = [FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_%#",proposal, suffix]];
if([fm fileExistsAtPath:path]){
[fm removeItemAtPath:path error:&error];
}
}
Note: paths and docmentsDirectory were not used in the question code and are not included in the answer.
I have created some directories in NSDocumentDirectory as
if (![tbxCreateFolder.text isEqualToString:#""]) {
NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryStr = [arrPaths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectoryStr stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#",tbxCreateFolder.text]];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]){
NSLog(#"success");
//Perfrom DB Insertation
}
else
{
NSLog(#"[%#] ERROR: attempting to write create MyFolder directory", [self class]);
NSAssert( FALSE, #"Failed to create directory maybe out of disk space?");
}
}
}
Now i want to get the list of directories that i have created in document folder but response is
NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePathString = ([arrPaths count] > 0) ? [arrPaths objectAtIndex:0] : nil;
NSLog(#"dirContents %#",basePathString);
Response is:
/Users/amir/Library/Application Support/iPhone Simulator/6.1/Applications/7003E8D5-CCBB-4E54-896B-F4BD2F4D2CB1/Documents
Here is the code:
NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// create directory named "test"
[[NSFileManager defaultManager] createDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:#"test"] withIntermediateDirectories:YES attributes:nil error:nil];
// retrieved all directories
NSLog(#"%#", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]);
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil];
BOOL isDir = NO;
for (NSString *path in paths) {
if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) {
// path is directory
}
}
You could always enumerate the contents of the top level directory using -fileExistsAtPath:isDirectory: like this:
NSFileManager *sharedFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *files = [sharedFileManager enumeratorAtPath:yourPath];
for (NSString *path in files) {
BOOL isDirectory = NO;
[sharedFileManager fileExistsAtPath:path isDirectory:&isDirectory];
if (isDirectory) {
// you found a directory, so do what you will with it
}
}
You can do this with few simple lines of code
NSString *documentDirectoryPath = // Initialize with Documents path
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *files = [fileManager contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
NSMutableArray *directoriesPaths = [NSMutableArray array];
for (NSString *filePath in files) {
if ([fileManager fileExistsAtPath:filePath isDirectory:&isDir] && isDir) {
[directoriesPaths addObject:filePath];
}
}
NSLog(#"directories %#", directoriesPaths);
try this,
NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strFilePath = [[arrPath objectAtIndex:0]
[[NSFileManager defaultManager] createDirectoryAtPath:[strFilePath stringByAppendingPathComponent:#"Sample"] withIntermediateDirectories:YES attributes:nil error:nil];