I have created a directory inside my project using this code
NSError * error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/createDirectory1"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSLog(@"Create a new Dir");
}
else
{
NSLog(@"Dir already exist");
}
now I want to check whether the directory exist or not? how can I get that through objective c code ?
NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/createDirectory1", docPath];
BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (!fileExists)
{
}
else
{
// Your Code
}
firstly,you should get the project path .you can do it like the codes bellow
NSString *getProjectPath(){
NSString *projectPath = [NSString stringWithFormat:@"%s,__FILE__]; // your project path may have some same prefix to the macro __FILE__.
// do something
return projectPath;
}
then you use [[NSFileManager defaultManager] fileExistsAtPath:dataPath] check if it exist
BOOL fileOrDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:dataPath];
fileExistsAtPath:dataPath
method returns a Boolean value that indicates whether a file or directory exists at a specified path. you are already doing it, as remus said.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.