I have this code and error with text:
ERROR: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x145b5a60 {NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSUserStringVariant=(
Move
), NSFilePath=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSDestinationFilePath=/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Documents/Files/temp.zip, NSUnderlyingError=0x1457af70 "The operation couldn’t be completed. No such file or directory"}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *pathDocumentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dictionaryName = #"Files";
NSString *filesPath = [[pathDocumentDirectory objectAtIndex:0] stringByAppendingPathComponent:dictionaryName];
NSString *toPath = [filesPath stringByAppendingPathComponent:#"temp.zip"];
NSString *fromPath = downloadedItem.contentURL.path;
NSError *error = nil;
BOOL isDir = YES;
if([fileManager fileExistsAtPath:fromPath])
{
// Create PurchasedBooks directory
if (![fileManager fileExistsAtPath:dictionaryName isDirectory:&isDir]) {
[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
}
[[NSFileManager defaultManager]moveItemAtPath:fromPath toPath:toPath error:&error];
if (error) {
NSLog(#"ERROR: %#", error.description);
}
}
Really do not understand why this happens
Add error checking for:
[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
Related
I am trying to release cache from files by calling this method :
-(void)removeCacheFromFiles
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSError *error=nil;
if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] error: &error] == YES)
NSLog (#"Remove successful");
else
NSLog (#"Remove failed");
NSLog(#"error %#",error);
[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
}
But I get "Remove failed" every time.
I tried this as well and get the same result:
-(void)removeCacheFromFiles
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *cachePathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePathList objectAtIndex:0];
NSError *error=nil;
if ([filemgr removeItemAtPath:cachePath error: &error] == YES)
NSLog (#"Remove successful");
else
NSLog (#"Remove failed");
NSLog(#"error %#");
[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
}
Also where should this remove caching be down ? Is it a good practise to call it in ViewDidDisappear or in ViewDidAppear so that the cache is cleared when the view controller is called the second time ?
The error in the NSLog says :
error Error Domain=NSCocoaErrorDomain Code=513 "“Caches” couldn’t be removed because you don’t have permission to access it." UserInfo={NSUnderlyingError=0x15fac9a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, NSFilePath=/var/mobile/Containers/Data/Application/1795C59B-185F-4607-8682-004CDF3CDED7/Library/Caches, NSUserStringVariant=(
Remove
)}
I am trying to create two log files and replace content in the second file with the content in the first file.
My code for creating log in AppDelegate
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory3 = [paths3 objectAtIndex:0];
logPath3 = [documentsDirectory3 stringByAppendingPathComponent:#"console4.log"];
if(![[NSFileManager defaultManager] fileExistsAtPath:logPath3])
[[NSFileManager defaultManager] createFileAtPath:logPath3 contents:[NSData data] attributes:nil];
NSLog(#"path %#",logPath3);
freopen([logPath3 cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
logPath2 = [documentsDirectory2 stringByAppendingPathComponent:#"console3.log"];
if(![[NSFileManager defaultManager] fileExistsAtPath:logPath2])
[[NSFileManager defaultManager] createFileAtPath:logPath2 contents:[NSData data] attributes:nil];
NSLog(#"path %#",logPath2);
freopen([logPath2 cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
_isFileOneCreated =YES;
code to move content
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSError * err;
if ([filemgr moveItemAtPath:
logPath2 toPath:
logPath3 error: &err])
NSLog (#"Day 3 Move successful");
else
NSLog (#"Day 3 Move failed %#",[err localizedDescription]);
But i get an error 516.
(Cocoa error 516.)" UserInfo=0x16dba220 {NSSourceFilePathErrorKey=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console3.log, NSUserStringVariant=(
Move
), NSFilePath=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console3.log, NSDestinationFilePath=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console4.log, NSUnderlyingError=0x16da63f0 "The operation couldn’t be completed. File exists"}
Help me solve my issue
moveItemAtPath does not allow you to overwrite the file with same name. See this Question Thread.
All you need to do is delete the file at target location before you overwrite with new one. Use the below code. This will do the trick but i advice you to take precaution to have the backup of your file just in case move doesn't work for some unknown reason.
NSFileManager *filemgr = [NSFileManager defaultManager];
NSError * err;
[filemgr removeItemAtPath:logPath3 error:&err];
if ([filemgr moveItemAtPath:logPath3 toPath:logPath2 error: &err])
NSLog (#"Day 3 Move successful");
else
NSLog (#"Day 3 Move failed %#",[err localizedDescription]);
I try to get my file in my self created Directory in the Documents Directory into a NSData Object like this:
NSString *path = [documentsDirectory stringByAppendingFormat:#"%#%#",#"/",fileName];
NSError* errorr = nil;
NSData *fileData = [NSData dataWithContentsOfFile:path options: 0 error: &errorr];
if (fileData == nil)
{
NSLog(#"Failed to read file, error %#", errorr);
}
else
{
}
But i always get this error:
Failed to read file, error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x156be110 {NSFilePath=/var/mobile/Applications/679253E3-652C-45EE-B589-609E52E4E3B0/Documents/upload/test.xml, NSUnderlyingError=0x156ba7f0 "The operation couldn’t be completed. Permission denied"}
So if i check if the file is readable:
if ([[NSFileManager defaultManager] isReadableFileAtPath: path] == YES)
NSLog (#"File is readable");
else
NSLog (#"File is read only");
i get the result that the file is readable, so why do i get the error if i want to parse that file into NSData?!
UPDATE: i created my file like this:
NSString *filePath = [self dataFilePath:fileName];
[xmlData writeToFile:filePath atomically:YES];
- (NSString *)dataFilePath: (NSString *) path {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"upload"];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error];
NSString *documentsPath = [documentsDirectory
stringByAppendingPathComponent:path];
return documentsPath;
}
UPDATE2: After creating the file, i move it into another Directory with this function:
- (void)moveXmlFilesToUploadDirectory
{
#try {
//Check if FILES_DIR exists
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:FILES_DIR];//filesDir
NSString *uploadDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:UPLOAD_DIR];//filesDir
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
{
NSString *extension = #"xml";
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[[NSFileManager defaultManager] moveItemAtPath:documentsDirectory toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];
}
}
}
}
#catch (NSException *exception) {
}
#finally {
}
}
Maybe the Problem is after moving the file! Cause if i try to get my file into a NSData before i move it, everything works...
It seems that you try to copy the whole directory to a destination meant to be a file. The result is that the filepath is a directory path in the end so you can't open it like a file which leads to the permission error message.
You should update your copy code to
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[[NSFileManager defaultManager] moveItemAtPath:[documentsDirectory stringByAppendingPathComponent: filename] toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];
}
}
to copy every file individually. That will create than the actual file at the destination path intend of a directory.
The chances are high that you don't have the right permissions for that file. Please check the permissions and add read access for the current user or "everyone". You can set the read permissions for your user by the info dialog (cmd + i) or in the terminal with chmod u+r test.xml. Eventually you have to set the owner before with
sudo chown yourusername test.xml
Hope that makes sense.
I am trying to copy a file from one folder to another folder in documents directory like this:
- (void)saveFile:(NSString *)folderPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"destination.png"];
NSError *error = nil;
NSString *srcPath = [folderPath stringByAppendingPathComponent:#"filename.png"];
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
if (error)
{
NSLog(#"%#",error);
}
if (success == YES)
{
NSLog(#"Copied");
}
else
{
NSLog(#"Not Copied");
}
}
When i log error, it gives me the following message:
Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be completed. (Cocoa error 516.)" UserInfo=0x9d665a0 {NSUserStringVariant=(
Move
), NSFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents/foldername/B713320C-2CA0-4FD3-93F6-71D76B102B83/src.png, NSDestinationFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents, NSUnderlyingError=0x9d41c60 "The operation couldn’t be completed. File exists"}
Issue 1:
The issue occurred because, the directory already contains a file with same file name.
You should check whether the file exist in the folder or not like:
- (void)saveFile:(NSString *)folderPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"destination.png"];
NSError *error = nil;
NSString *srcPath = [folderPath stringByAppendingPathComponent:#"filename.png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
//removing file
if (![[NSFileManager defaultManager] removeItemAtPath:dataPath error:&error])
{
NSLog(#"Could not remove old files. Error:%#",error);
}
}
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
if (success == YES)
{
NSLog(#"Copied");
}
else
{
NSLog(#"Not Copied %#", error);
}
}
Issue 2:
You must provide a file name not a directory.
Replace:
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
With:
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
copyItemAtPath: is giving error as that file is already exist in that location. So you should do this before you copy file.
if ([[NSFileManager defaultManager] fileExistsAtPath: srcPath])
[[NSFileManager defaultManager] removeItemAtPath: srcPath error:nil];
Replace this line:
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
With:
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
Here is the code I have for downloading an archive and saving it to my Documents folder:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:resource] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
NSData *receivedData;
NSURLResponse *requestResponse;
NSError *requestError;
int i = 3;
while (i) //Try 3 times
{
receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&requestError];
if (receivedData)
{
i = 0;
}
else i--;
}
if (!receivedData)
{
NSLog(#"************************************************");
NSLog(#"** Error downloading data for resource : %# **", resource);
NSLog(#"************************************************");
}
else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents folder
NSString *dataPath;
dataPath = [documentsDirectory stringByAppendingPathComponent:LIST_FOLDER];
NSError *documentError;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) //Create folder
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&documentError];
}
NSLog(#"saveError = %#", documentError); //Prints out null so no problem here
NSError *writeError;
[receivedData writeToFile:dataPath options:NSDataWritingAtomic error:&writeError];
NSLog(#"write error = %#", writeError);
The last nslog prints out the following and won't save the archive:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" "The operation couldn’t be completed. Is a directory"}
The subfolder in the Documents directory gets created but it is empty.
Thanks
You are trying to write the data to the folder. You need to write it to a file in the folder.
dataPath is the directory you create. You need to append a filename to that path and then write the file to that full path (with filename).