Old 5th May 2011, 15:02   #1
JohnChen
Member
 
Join Date: Mar 2011
Posts: 80
How to check if it is a directory or a file?

Is there way to do that by using IfFileExists? Thanks.
JohnChen is offline   Reply With Quote
Old 5th May 2011, 15:15   #2
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Use \*.* on the end to check a path is a directory rather than a file.

Stu
Afrow UK is offline   Reply With Quote
Old 5th May 2011, 15:22   #3
JohnChen
Member
 
Join Date: Mar 2011
Posts: 80
Quote:
Originally Posted by Afrow UK View Post
Use \*.* on the end to check a path is a directory rather than a file.

Stu
That is a clever idea. But how to check a file rather than a directory? Obviously we can't use $IfFileExists File, because here File could be either a file or a directory. Am I right?
JohnChen is offline   Reply With Quote
Old 5th May 2011, 15:27   #4
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Swap the logic around then.

${If} ${FileExists} Path
${AndNot} ${FileExists} Path\*.*
# Is a file
${EndIf}

Stu
Afrow UK is offline   Reply With Quote
Old 5th May 2011, 15:36   #5
JohnChen
Member
 
Join Date: Mar 2011
Posts: 80
Quote:
Originally Posted by Afrow UK View Post
Swap the logic around then.

${If} ${FileExists} Path
${AndNot} ${FileExists} Path\*.*
# Is a file
${EndIf}

Stu
But {AndNot} is a valid command...
JohnChen is offline   Reply With Quote
Old 5th May 2011, 15:37   #6
JohnChen
Member
 
Join Date: Mar 2011
Posts: 80
Quote:
Originally Posted by JohnChen View Post
But {AndNot} is a valid command...
I meant {AndNot} is NOT a valid command.
JohnChen is offline   Reply With Quote
Old 5th May 2011, 15:38   #7
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
I was hoping you would look it up AndIfNot.

Stu
Afrow UK is offline   Reply With Quote
Old 5th May 2011, 15:46   #8
JohnChen
Member
 
Join Date: Mar 2011
Posts: 80
Quote:
Originally Posted by Afrow UK View Post
I was hoping you would look it up AndIfNot.

Stu
Sorry, I should have figured out. Thanks a lot.
JohnChen is offline   Reply With Quote
Old 5th May 2011, 17:14   #9
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
...Man, now you made me remember back when LogicLib still used ${AndUnless}.
MSG is offline   Reply With Quote
Old 6th May 2011, 03:03   #10
jiake
Senior Member
 
jiake's Avatar
 
Join Date: Oct 2007
Location: Shanghai, China
Posts: 207
Hi all, I have tested these two lines long ago :
IfFileExists "C:\AnEmptyFolderName\*.*" 0 +2
MessageBox MB_OK "C:\AnEmptyFolderName is a directory!"
IfFileExists "C:\AnEmptyFolderName" 0 +2
MessageBox MB_OK "C:\AnEmptyFolderName is a directory!"
I get the same result. It will show you two same message boxes.
That is to say whether the path is a file name or a directory, the results are the same only if it is really existed.

If you want to determine whether a path is directory or not, I think you must use PathIsDirectory Function.
System::Call `shlwapi::PathIsDirectory(t)i("C:\AnEmptyFolderName").R0`
${If} $R0 <> 0
MessageBox MB_OK Path `"C:\AnEmptyFolderName" is a directory!`
${EndIf}

Contact me: 137729898@qq.com
jiake is offline   Reply With Quote
Old 6th May 2011, 05:56   #11
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Quote:
Originally Posted by jiake View Post
Hi all, I have tested these two lines long ago :
IfFileExists "C:\AnEmptyFolderName\*.*" 0 +2
MessageBox MB_OK "C:\AnEmptyFolderName is a directory!"
IfFileExists "C:\AnEmptyFolderName" 0 +2
MessageBox MB_OK "C:\AnEmptyFolderName is a directory!"
I get the same result. It will show you two same message boxes.
Err... Yes, and that's exactly what should happen. The directory exists, so both Ifs return true. If a file is NOT a directory, THEN only one of the two ifs will be true. That's how you distinguish files from directories. (As Stu has already explained, in his post.)

There's really no need to call PathIsDirectory or anything like that.
MSG is offline   Reply With Quote
Old 6th May 2011, 09:27   #12
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Yes, with your code if AnEmptyFolderName is a directory you get both message boxes. If it is a file, you only get one (the 2nd). (And yes I just tried it to be sure ).

Stu
Afrow UK is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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

Forum Jump