Sign in to follow this  
Followers 0
nitro322

How to distinguish between files and directories?

5 posts in this topic

Gentlemen, I have yet another one of those odd problems that only seem to affect me. :-)

I'm writing a program that allows users to pass filenames to it. However, I need to verify that it 1) exists, and 2) is actually a file and not a directory. FileExists() will take care of condition 1), but I haven't been able to figure out how to handle condition 2). Any suggestions?

Thanks.

Oh, I just thought of a possible useful feature. How about modifying FileExists() to return 0 if the passed filename does not exists, 1 if it does and it's a file, or 2 if it does and it's a directory? This would offer a very simple solution for anyone that needs to distinguise between the two. Also, since either 1 or 2 will both be considered TRUE, it shouldn't have any unintentional side-effects (unless someone hard-coded the 1).

Of course, that doesn't help me here. :-) So, any suggestions? Again, thanks.

Share this post


Link to post
Share on other sites



there are lots of ways...

heres one idea

$File = "the name give by the user

; check if folder exists
DirGetSize($File)
If @error= 1 Then
; the folder does not exist
EndIf

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

FileGetAttrib() correctly recognizes a directory.

$attrib = FileGetAttrib(@WindowsDir)
If not @error Then
    If StringInStr($attrib, "D") Then MsgBox(4096,"", "Source is a Directory.")
EndIf

Share this post


Link to post
Share on other sites

MHz, FileGetAttrib() will work perfectly. I actually looked into it earlier, but somehow managed to miss the Directory attribute. Naturally.

Valuater, I hadn't thought about that approach. Will keep it in mind for the future.

Thanks for the help.

Share this post


Link to post
Share on other sites

Thanks for the help.

No problem. Thankyou for the nice gesture. :lmao:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0