This could be easy, but I am not getting the problem.
I am using below code to rename the folders of document directory and is working fine except one case.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Photos"];
NSArray * arrAllItems = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL]; // List of all items
NSString *filePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", [arrAllItems objectAtIndex:tagSelected]]];
NSString *newDirectoryName = txtAlbumName.text; // Name entered by user
NSString *oldPath = filePath;
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newDirectoryName];
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
if (error) {
NSLog(#"%#",error.localizedDescription);
// handle error
}
Now, my problem is if there is a folder named "A"(capital letter A) and I am renaming it to "a" (small letter a), then it is not working and giving an error. I am not getting where the problem is.
The HFS+ file system (on OS X) is case insensitive, but case preserving.
That means if you create a folder "A" and then check if there is a folder "a", you will get
"yes" as an answer.
The file manager moveItemAtPath:toPath:... checks first if the destination path already
exists and therefore fails with
NSUnderlyingError=0x7126dc0 "The operation couldn’t be completed. File exists"
One workaround would be to rename the directory to some completely different name first:
A --> temporary name --> a
But a much easier solution is to use the BSD rename() system call, because that
can rename "A" to "a" without problem:
if (rename([oldPath fileSystemRepresentation], [newPath fileSystemRepresentation]) == -1) {
NSLog(#"%s",strerror(errno));
}
Note that the problem occurs only on the iOS Simulator, not on the device, because
the device file system is case sensitive.
Swift:
let result = oldURL.withUnsafeFileSystemRepresentation { oldPath in
newURL.withUnsafeFileSystemRepresentation { newPath in
rename(oldPath, newPath)
}
}
if result != 0 {
NSLog("%s", strerror(errno))
}
Related
In my iOS app I would like that user can download some jpg file via iTunes. So I've enabled UIFileSharingEnabled. But users are now able to put files in my app. I would like to block that. Is there a way to do that ?
Thanks !
Don't think you can block it, but you can just delete unwanted files when your app becomes active.
Put some code a bit like the sample below - filling in the test to avoid deleting the files you want to be available in iTunes.
Call this from within applicationDidBecomeActive: in your application delegate.
If you're more cautious you might want to check the user hasn't dropped a jpg file with the same name as the one you've parked there. You could test for sameness of date or some such or, if you've not got many files, just delete everything and write them again when the app becomes active.
- (void) removeUnwantedFiles;
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray* directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inboxPath error:NULL];
if (!directoryContents || [directoryContents count] == 0)
{
return;
}
for (NSString* fileName in directoryContents)
{
if ( /* some test of filename to see if it's one of my kosher files */ ) continue;
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSError* error = nil;
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
// NSLog(#"Deleting (%#): %#", success ? #"succeeded" : #"failed", [filePath lastPathComponent]);
if (!success)
{
NSLog(#"Error: %#", [error localizedDescription]);
}
}
}
in my iPhone app I'm using file manager to check size of file,code is working fine on simulator, but when I run the same app on iphone it is saying file size is zero even though file size is greater than zero, I am using following code:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
documentsDirectory1 = [documentsDirectory1 stringByAppendingPathComponent:#"XML"];
NSString * szDestPath1 = [documentsDirectory1 stringByAppendingPathComponent:#"Extras"];
NSString *URL2 = [szDestPath1 stringByAppendingPathComponent:#"config.xml"];
NSLog(#"%#",URL2);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL2 error:&attributesError];
int _fileSize = [fileAttributes fileSize];
NSLog(#"_fileSize:%U",_fileSize); //0 if empty
How can I solve this, can anyone help me? thanx in advance.
Are you sure that the file exists?
Usually if a piece of code that involves files works fine on the simulator but not on the device it is because of wrong filenames. Most simulators run on a case-insensitive filesystem, but the device has a case-sensitive filesystem.
So please check again if you use the correct file path.
You could add something like this to make sure that the file exists:
if (![[NSFileManager defaultManager] fileExistsAtPath:URL2]) {
NSLog(#"File does not exist");
}
In the Simulator I can save an NSMutableArray to a file and read it back with the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:#"RiskValues"]){ // If file exists open into table
NSLog(#"Risk Values File Exists");
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"RiskValues", documentsDirectory];
gRiskValues = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
gRiskValuesAlreadyInitialised = YES;
} else {
NSLog(#"Can't find RiskValues file, so initialising gRiskValues table");
Do something else .......
}
This doesn't work on the device. I have tried to locate the file using the following but it still doesn't work:
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent#"RiskValues"];
What am I doing wrong?
Great answers from everyone. I have resolved the file path and existence issues at a stroke. Many, many thanks.
You have to provide absolute path here:
if ([fileManager fileExistsAtPath:#"RiskValues"])
So it must look like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:#"RiskValues"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: fullFileName]){ // If file exists open into table
NSLog(#"Risk Values File Exists");
gRiskValues = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
gRiskValuesAlreadyInitialised = YES;
} else {
NSLog(#"Can't find RiskValues file, so initialising gRiskValues table");
Do something else .......
}
NSString *fullFileName = [NSString stringWithFormat:#"RiskValues", documentsDirectory];
this line, you're not creating your full path string right. what you should do is
NSString *fullFileName = [documentsDirectory stringByAppendingPathComponent:#"RiskValues"];
also this check
if ([fileManager fileExistsAtPath:#"RiskValues"])
Will never pass on iOS as it is not a full path to any place you are allowed to write at in your sandbox. I suppose it works on the simulator because on the mac it's looking up relatively to the HD root (or something, not sure how the mac file system works :) ), but on the iOS you're going to have to give it a path to a file/directory in your documents (maybe by appending #"RiskValues" to it or whatever)
1) [NSString stringWithFormat:#"RiskValues", documentsDirectory] is just #"RiskValues". So this name points to file in application's directory.
2) [fileManager fileExistsAtPath:#"RiskValues"] searches for file in application directory. It's available for read/write in simulator (it's in your computer file system after all) but it's read-only on device.
BTW (NSFileManager Class Reference)
Attempting to predicate behavior based
on the current state of the file
system or a particular file on the
file system is not recommended. Doing
so can cause odd behavior in the case
of file system race conditions. It's
far better to attempt an operation
(such as loading a file or creating a
directory), check for errors, and
handle any error gracefully than it is
to try to figure out ahead of time
whether the operation will succeed.
Solution:
1) Do not check file presence. Just try to make dictionary with initWithContentsOfFile:encoding:error:
2) You want it to be in documents directory so construct path like this
[documentsDirectory stringByAppendingPathComponent:#"RiskValues"];
I am supposed to have downloaded a file on the iPad simulator, using an appication.
How can I check that this file is present on the iPad simulator file system ?
Is there some file manager that I can use to check the existence of the file and possibly its contents?
You can find the simulator at
/Username/Library/Application
Support/iPhone Simulator/4.2/
where "4.2" is the SDK version.
Under that folder you find all applications in cryptical subfolders. One of them is yours.
In there you'lss find "Documents" which is your app's personal folder.
Use these methods, you can verify a file is present in the documents directory. I've not tested this exact example but it should work.
Example:
NSString *filePath = [self dataFilePathInDocuments:#"MyFile.txt"];
if([self fileExistsAtPath:filePath]) {
NSLog(#"The file exits at %#", filePath);
} else {
NSLog(#"The doesn't exist at %#", filePath);
}
Code:
- (NSString *)dataFilePathInDocuments:(NSString*)aFilename {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:aFilename];
}
- (BOOL) fileExistsAtPath:(NSString*)aFilepath {
NSFileManager *fm = [NSFileManager defaultManager];
return [fm fileExistsAtPath:aFilepath];
}
i am new to iPhone programming.
What is the proper way to check that whether a file is exist or not?
BOOL isDirectory = NO;
if ( [[NSFileManager defaultManager]
fileExistsAtPath:path
isDirectory: &isDirectory ]) {
// file already exists
} else {
// file does not yet exist
}
To put it in more detail:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]){
//Do something...
}
You can append the actual file name to "documentsDirectory" like this: [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg", spidermanpic]].
The isDirectory option in the answer above is used to check if the path is a directory or a file. Please keep in mind that it is a pointer. It wont work without the "&".