Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if file exist

Tags:

xcode

iphone

i want to check a folder.if i found "test.jpeg" in "path" if it 's true i do nothing but if it false i have to download this picture like that

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dico objectForKey:@"photo"]]]];
nomPhoto = [[cell.pseudo text]stringByReplacingOccurrencesOfString:@"\n" withString:@""];;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *document = [paths objectAtIndex:0];
filename = [NSString stringWithFormat:@"%@/%@.jpeg",document,nomPhoto];

NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.1f)];//1.0f = 100% quality

[data2 writeToFile:filename atomically:YES];

EDIT: i try this but don't work. the path is good

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* pict =  [documentsPath stringByAppendingPathComponent :@"portos"]; 


BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pict];

if (fileExists)
{
    NSLog(@"file ok");
}else {
    NSLog(@"file ko");
}

thx

like image 330
XcodeMania Avatar asked Jul 21 '11 07:07

XcodeMania


People also ask

How check file exist in Python?

In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.

How do I check if a file exists in Linux?

When checking if a file exists, the most commonly used FILE operators are -e and -f . The first one will check whether a file exists regardless of the type, while the second one will return true only if the FILE is a regular file (not a directory or a device).

How do I check if a file exists in HTML?

html file exists in the current folder, you can write the following code: const fs = require("fs"); const path = "./index. html"; fs. exists(path, function (isExist) { if (isExist) { console.


2 Answers

Its already answered here.

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];
like image 70
Praveen S Avatar answered Oct 11 '22 20:10

Praveen S


If the file specified doesn't exist the CGImage property of UIImage will be nil.

if (image.CGImage) NSLog(@"file ok");
else NSLog(@"file ko");
like image 37
RTasche Avatar answered Oct 11 '22 19:10

RTasche