|
-
Dec 7th, 2001, 04:59 PM
#1
Thread Starter
Member
MY FileExists function
Since in the last few days there have been (for some reason) a lot of questions about whether a file exists, try this:
VB Code:
Public Function FileExists(Filename As String, _
Optional TreatDirAsFile As Boolean = False) As Boolean
' Returns true if Filename exists. If Filename is a directory and
' TreatDirAsFile is true then the function will also return true.
' Similarly if the file is really a directory and TreatDirAsFile is false
' then the function will return false.
On Error Goto FileIOError ' catch a 404
Dim Attributes As Integer
Attributes = GetAttr(Filename) ' will throw an error if it doesn't exist
If Attributes = vbDirectory And Not TreatDirAsFile Then
FileExists = False ' it's a directory but that's not cool
Else
FileExists = True ' it's a directory but we said that's cool
End If
Exit Function
FileIOError:
FileExists = False ' error thrown at GetAttr, file doesn't exist
End Function
The TreatDirAsFile will make the function return true if Filename is a directory. But I haven't tested it, does it work?
Last edited by filburt1; Dec 7th, 2001 at 05:04 PM.
-
Dec 7th, 2001, 05:01 PM
#2
Can I know why you do not have vb?
-
Dec 7th, 2001, 05:02 PM
#3
Thread Starter
Member
I'm at work and I refuse to open the VBA editor.
-
Dec 7th, 2001, 05:03 PM
#4
It work LIKE A CHARM!!!!!!!!!!!!!!!!!!! it's so damn sexy!!! but that code in your tips page
-
Dec 7th, 2001, 05:05 PM
#5
Thread Starter
Member
I think I will...later. I'm going to wait for TurtleTips.com to go live first before I do more web stuff probably.
-
Dec 7th, 2001, 05:36 PM
#6
Thread Starter
Member
Anybody else care to comment or see a bug?
-
Dec 7th, 2001, 06:09 PM
#7
Not bugs, but I would add some code the make sure what was in Filename. In other words, check to see if it's something like "C:\path\myfile" or just "myfile". I might also consider returning a long with the following values:
-1 means file does not exist
0 means file exists
any other value is the err.number in case an error occurs.
-
Dec 7th, 2001, 06:17 PM
#8
Frenzied Member
Uhh i was just wondering why you can't use the Dir() function? It seems to support UHC paths and everything...?
You just proved that sig advertisements work.
-
Dec 7th, 2001, 06:19 PM
#9
Thread Starter
Member
It will say that the file "*" exists and other stuff.
-
Dec 7th, 2001, 06:25 PM
#10
Frenzied Member
Originally posted by filburt1
It will say that the file "*" exists and other stuff.
What?
You just proved that sig advertisements work.
-
Dec 7th, 2001, 07:22 PM
#11
Thread Starter
Member
The Dir() function works for filters, so if you asked if the file "*.txt" exists then it will return true, even though "*.txt" isn't a valid filename because of the wildcard.
And let's not even mention the FSO.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|