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.
Related
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);
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
}
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];
In my app I download a pdf file with an ASiHttpRequest and I have these instructions:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:#"file.pdf"]];
it work fine at first time, and I can open this file.pdf, but when I download a second time this pdf, it seems that it not replace the file but do a merge.
before I do this, but it doesn't work where is the problem, or what's the best way to delete this file.pdf from its path?
- (void) removeFile{
NSString *extension = #"pdf";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPDF = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPDF error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[documentsDirectoryPDF stringByAppendingPathComponent:filename] error:NULL];
}
}
}
EDIT
now I use this method
- (void) removeFile{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0]stringByAppendingString:#"/file.pdf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"Documents directory before: %#", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
if([fileManager fileExistsAtPath:path] == YES)
{
NSLog(#"file exist and I delete it");
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:path error:&error];
NSLog(#"error:%#", error);
}
NSLog(#"Documents directory after: %#", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
}
this method recognize that in directory there is "file.pdf" in NSLog
NSLog(#"Documents directory before: %#", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
but it crash after
"NSLog(#"file exist and I delete it");"
and I have only a "lldb" in consolle.
I use this method to delete pdf files from a local cache, with a few modifications you can adapt it to your necessities
- (void)removePDFFiles
{
NSFileManager *fileMngr = [NSFileManager defaultManager];
NSArray *cacheFiles = [fileMngr contentsOfDirectoryAtPath:[self cacheDirectory]
error:nil];
for (NSString *filename in cacheFiles) {
if ([[[filename pathExtension] lowercaseString] isEqualToString:#"pdf"]) {
[fileMngr removeItemAtPath:[NSString stringWithFormat:#"%#/%#", [self cacheDirectory], filename] error:nil];
}
}
}
Most probably you don't remove the file before downloading a new one and ASIHttpRequest sees that there's already a file with the same name and appends data to it instead of replacing the file. I'm not sure about the PDF format, but that shouldn't normally result in a merged readable file. In any case, first you need to use the error mechanism that the filemanager class offers you. Is bad to pass NULL. Very bad. So, create a NSError object and pass it to the contentsOfDirectoryAtPath and removeItemAtPath methods, then check the error, be sure the operations are done successfully. After that, you may want to check the extension upper case as well, as Unix based systems are case sensitive (although the simulator is not, the device is) and a example.PDF file will not get deleted based on your code.
Try the following:
-(BOOL) removePDF
{
BOOL removeStatus = NO;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* fileName = #"file.pdf"; //your file here..
NSString* filePath = [NSString stringWithFormat:#"%#/%#", docsDir, fileName];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == YES)
{
removeStatus = [[NSFileManager defaultManager] removeItemAtPath:filePath];
}
return removeStatus;
}