Archive: How to check if it is a directory or a file?


How to check if it is a directory or a file?
Is there way to do that by using IfFileExists? Thanks.


Use \*.* on the end to check a path is a directory rather than a file.

Stu


Originally posted by Afrow UK
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?

Swap the logic around then.

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

Stu


Originally posted by Afrow UK
Swap the logic around then.

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

Stu
But {AndNot} is a valid command...

Originally posted by JohnChen
But {AndNot} is a valid command...
I meant {AndNot} is NOT a valid command.

I was hoping you would look it up :) AndIfNot.

Stu


Originally posted by Afrow UK
I was hoping you would look it up :) AndIfNot.

Stu
Sorry, I should have figured out. Thanks a lot.

...Man, now you made me remember back when LogicLib still used ${AndUnless}. :)


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}


Originally posted by jiake
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.

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