Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > fileExistsAtPath:isDirectory

fileExistsAtPath:isDirectory
Thread Tools
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 16, 2002, 07:48 PM
 
there is some sample code for this function that goes like this...
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> NSArray *subpaths;
BOOL isDir;
NSString *fontPath = @"/System/Library/Fonts";
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir)
subpaths = [manager subpathsAtPath:fontPath]; </font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">my problem is it always comes back as true. Even when it is not.

Is apple's code wrong?
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Jul 16, 2002, 08:04 PM
 
well, that code is correct. but can we see your actual code?

might be you're doing something differently there.
     
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 16, 2002, 08:26 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by seb2:
<strong>well, that code is correct. but can we see your actual code?

might be you're doing something differently there.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">That is what bothers me I am doing almost nothing with it at this point...
here is one of the ways I used it...
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> if([manager fileExistsAtPath:moviesLoc isDirectory:&isDir] && isDir)
dirCheck = 1;
else
dirCheck = 0; </font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I then use dirCheck in a switch statement, but no matter what it always goes to 'default'... <img border="0" alt="[Hmmm]" title="" src="graemlins/hmmm.gif" />
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">switch(dirCheck)
{
case '1': //user selected a directory
break;
case '0': //user selected a single file
break;
default: //shouldn't need this but the switch always resolves to it.
break;
}</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I am using NSLog to see what is happening. So I know it's not the code I cut out....
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 16, 2002, 08:57 PM
 
Sorry if I'm totally off base, but

'0' != 0.

Maybe you could do

switch ([manager fileExistsAtPath:moviesLoc isDirectory:&isDir] && isDir)
{
case 0:
case 1:
}

<small>[ 07-16-2002, 10:15 PM: Message edited by: skipjack ]</small>
     
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 16, 2002, 09:44 PM
 
well ****! Had to lookup switch syntax, last time I get help from my prof's book

But I still have my original problem. If I select a directory or a file it always thinks I selected a directory and goes into that part of the switch.
Any ideas?
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Eastern Washington (St. John/Cheney)
Status: Offline
Reply With Quote
Jul 16, 2002, 09:47 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by skipjack:
<strong>Sorry if I'm totally off base, but

'0' != 0.

Maybe you could do

switch ([manager fileExistsAtPath:moviesLoc isDirectory:&isDir] && isDir)
{
case 0:
case 1:
}</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yeah, in C-derived languages, '0' is the ASCII character zero (with decimal value 48, whereas 0 (without single quotes) is the number zero (with decimal value 0, of course).

I would strong suspect that to be the problem.

CyberDave
     
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 16, 2002, 11:14 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by juanvaldes:
<strong>well ****! Had to lookup switch syntax, last time I get help from my prof's book

But I still have my original problem. If I select a directory or a file it always thinks I selected a directory and goes into that part of the switch.
Any ideas?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I have no idea. I can't even understand what it is doing! I mean, can you explain why the code example sets the Boolean and then does an AND with it? (I know you're just following the example. I'm looking at it too.)
EDIT: Oh. I understand now. I'm slow at this stuff.

THIS IS WHAT I WROTE ORIGINALLY:
What if you play around with it. Give the Boolean an initial value. Call the method separately and just put the Boolean in the if statement.

isPath = 0;
[manager fileExistsAtPath:fontPath isDirectory:&isDir];
if (isDir)

or maybe that makes no sense. How about adding a variable?

Boolean isMyDir;
isMyDir = [manager fileExistsAtPath: fontPath isDirectory:&isPath]
if (isMyDir)

Obviously, I don't really know what I'm doing. I'd probably just put a bunch of "cout " statements until I could see what was going on.

THIS IS MY LATEST GUESS:

Test the isDirectory method separately.

Boolean isPath;
[manager filesExistsAtPath: fontPath isDirectory:&isPath];
if (isPath)

or maybe just

cout &lt;&lt; "isPath = " &lt;&lt; isPath &lt;&lt; endl;

with fontPath and your movieLoc to see if Apple's method is doing what you expect.

<small>[ 07-17-2002, 12:32 AM: Message edited by: skipjack ]</small>
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Jul 17, 2002, 05:07 AM
 
well, i think the solution to your problem already is there. the only question left: why use a switch statement at all?

simply do a "if (dirCheck)" and you should be fine. the thing with switch statements is that many people tend to forget the "break", just unnecessary trouble.

to clarify: the fact that there *is* the possibility to leave away the "break" is useful in some cases, but mostly not what you want. and in your case, there's no way it could be anything but "1" or "0". to make it even shorter, instead of writing

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if([manager fileExistsAtPath:moviesLoc isDirectory:&amp;isDir] &amp;&amp; isDir)
dirCheck = 1;
else
dirCheck = 0;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">why not immediately write

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">dirCheck = ([manager fileExistsAtPath:moviesLoc isDirectory:&amp;isDir] &amp;&amp; isDir);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
     
Addicted to MacNN
Join Date: Mar 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 18, 2002, 09:21 PM
 
seb, yeah I did remember the break; and my code has mutated a bunch trying to get this to do what I expect.

skipjack, C++ cout statements are illegal in obj-c and don't work. I have been using NSLog, but I don't know how to have it output a variable. Anyone know how I would do that? ie NSLog(dirCheck), this crashes the app btw.

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> dirCheck = [manager fileExistsAtPath:moviesLoc isDirectory:&isDir];
if(dirCheck)
{
NSLog(@"we selected a directory");
}
else
{
NSLog(@"we did not select a directory");
}</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Does not seem to matter if I selected a file or directory, I always go into the if. <img border="0" alt="[Skeptical]" title="" src="graemlins/bugeye.gif" /> And dirCheck = FALSE; when it is declared.

Thanks again to everyone for all their help...
The spirit of resistance to government is so valuable on certain occasions, that I wish it always to be kept alive.
- Thomas Jefferson, 1787
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Eastern Washington (St. John/Cheney)
Status: Offline
Reply With Quote
Jul 18, 2002, 10:55 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by juanvaldes:
<strong>seb, yeah I did remember the break; and my code has mutated a bunch trying to get this to do what I expect.

skipjack, C++ cout statements are illegal in obj-c and don't work. I have been using NSLog, but I don't know how to have it output a variable. Anyone know how I would do that? ie NSLog(dirCheck), this crashes the app btw.

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> dirCheck = [manager fileExistsAtPath:moviesLoc isDirectory:&isDir];
if(dirCheck)
{
NSLog(@"we selected a directory");
}
else
{
NSLog(@"we did not select a directory");
}</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Does not seem to matter if I selected a file or directory, I always go into the if. And dirCheck = FALSE; when it is declared.

Thanks again to everyone for all their help...</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">You can use formatted strings with NSLog(), much like the good ol' printf() in ANSI C (with %@ being an additional formatter for NSStrings). This is how I output BOOLs in Cocoa:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSLog( @&quot;Value of dirCheck: %@&quot;, ( dirCheck ? @&quot;YES&quot; : @&quot;NO&quot; ) ); </pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">(There may be a boolean formatter for NSLog/printf, but I'm too lazy to go dig out a C book and see what it is. Actually, come to think of it, there might not be, since IIRC there is no "boolean" data type in ANSI C. I may be wrong, though.)

Also, don't forget that [manager fileExistsAtPath: isDirectory] indirectly returns whether or not the path specified is a directory through the isDirectory parameter. This means you should be checking isDir to see if it is a directory, not the direct return value of fileExistsAtPath (which is telling you if a file exists at the path specified). Note that Apple's code *is* using this with the "&& isDir" part in their if statements. Try this:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">dirCheck = [manager fileExistsAtPath:moviesLoc isDirectory:&amp;isDir];
if(isDir)
{
NSLog(@&quot;we selected a directory&quot;
}
else
{
NSLog(@&quot;we did not select a directory&quot;
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Also, for those of you who don't know, there is an open-source utility called Cocoa Browser which works a lot like the Java Browser utility Apple includes with the dev tools that makes it pretty easy to look up all the Cocoa documentation. The hard part is remembering which classes come from AppKit and which come from Foundation. Get it here: <a href="http://homepage.mac.com/hoshi_takanori/cocoa-browser/" target="_blank">http://homepage.mac.com/hoshi_takanori/cocoa-browser/</a>

Hope this helps,

CyberDave
     
Dedicated MacNNer
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 19, 2002, 12:32 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">
Also, for those of you who don't know, there is an open-source utility called Cocoa Browser which works a lot like the Java Browser utility Apple includes with the dev tools that makes it pretty easy to look up all the Cocoa documentation. The hard part is remembering which classes come from AppKit and which come from Foundation. Get it here: <a href="http://homepage.mac.com/hoshi_takanori/cocoa-browser/" target="_blank">http://homepage.mac.com/hoshi_takanori/cocoa-browser/</a>
</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">As a beginner to Cocoa, I stumbled across this app and OH-MY-GOD it made everything SO MUCH EASIER. This should be included with the Developer tools. It's the fastest, most clear and easy-to-use reference I've found. Much more convenient than looking through the HTML documentation.

As object oriented programming is new to me in general, just being able to peruse through it is helping me "get" how everything works. So in short, I LOVE it.

W
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:04 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2