|
-
Aug 22nd, 2002, 12:26 PM
#1
Thread Starter
Junior Member
Checking if a file exists
Given the directory path and fielname, how do I check in VB if the file exists. If it doesn't exist when I check for it, then it should only return a value (eg 0, False, etc) and not crash my program with a Runtime Error.
Thanks.
-
Aug 22nd, 2002, 12:28 PM
#2
Need-a-life Member
Asked a zillion times....
Dir Function
Returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.
Syntax
Dir[(pathname[, attributes])]
The Dir function syntax has these parts:
Part Description
pathname Optional.String expression that specifies a file name — may include directory or folder, and drive. A zero-length string ("") is returned if pathname is not found.
attributes Optional.Constant ornumeric expression, whose sum specifies file attributes. If omitted, returns files that match pathname but have no attributes.
Settings
The attributesargument settings are:
Constant Value Description
vbNormal 0 (Default) Specifies files with no attributes.
vbReadOnly 1 Specifies read-only files in addition to files with no attributes.
vbHidden 2 Specifies hidden files in addition to files with no attributes.
VbSystem 4 Specifies system files in addition to files with no attributes.
vbVolume 8 Specifies volume label; if any other attributed is specified, vbVolume is ignored.
vbDirectory 16 Specifies directories or folders in addition to files with no attributes.
Note These constants are specified by Visual Basic for Applications and can be used anywhere in your code in place of the actual values.
Remarks
Dir supports the use of multiple character (*) and single character (?) wildcards to specify multiple files.
Figure it out yourself... the only way to learn.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 27th, 2002, 06:59 AM
#3
VB Code:
If Not (Len(Dir$("C:\boot.ini")) <> 0) Then
MsgBox "File Doesn't Exist!"
Else
MsgBox "File Does Exist"
End If
-
Aug 27th, 2002, 07:37 AM
#4
Frenzied Member
DIR function do not work on network.
You must use API for clear that.
VB Code:
Option Explicit
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Function fcnFileExists(sSource As String) As Boolean
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sSource, WFD)
fcnFileExists = hFile <> INVALID_HANDLE_VALUE
Call FindClose(hFile)
End Function
oh1mie/Vic

-
Aug 27th, 2002, 08:15 AM
#5
Addicted Member
Real simple way to do it:
Call Len(File) and if it shoots up a runtime error, the file doesn't exist. You can catch that with the "On Error GoTo Label" statement.
Not at all related to sheep...
-
Aug 27th, 2002, 08:22 AM
#6
PowerPoster
Well
Originally posted by A$$Bandit
Real simple way to do it:
Call Len(File) and if it shoots up a runtime error, the file doesn't exist. You can catch that with the "On Error GoTo Label" statement.
Will this work for a network?
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Aug 27th, 2002, 08:24 AM
#7
-
Aug 27th, 2002, 08:26 AM
#8
Need-a-life Member
Re: Well
Originally posted by James Stanich
Will this work for a network?
I don't understand what he tried to write... but, as it's written, that doesn't work at all.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 27th, 2002, 08:31 AM
#9
PowerPoster
Well
oh1mie pointed out his way willl work when checking for existance over a network.
Will Len(File) work over a network ?
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Aug 27th, 2002, 08:34 AM
#10
Need-a-life Member
Re: Well
Originally posted by James Stanich
oh1mie pointed out his way willl work when checking for existance over a network.
Will Len(File) work over a network ?
I'm not sure about the Dir not working over a network. Anyway, the Len is for strings. That code does not work.
Len Function
Returns aLong containing the number of characters in a string or the number of bytes required to store avariable.
Syntax
Len(string | varname)
The Len function syntax has these parts:
Part Description
string Any validstring expression. If string containsNull, Null is returned.
Varname Any validvariable name. If varname contains Null, Null is returned. If varname is aVariant, Len treats it the same as a String and always returns the number of characters it contains.
Remarks
One (and only one) of the two possiblearguments must be specified. Withuser-defined types, Len returns the size as it will be written to the file.
Note Use the LenB function with byte data contained in a string, as in double-byte character set (DBCS) languages. Instead of returning the number of characters in a string, LenB returns the number of bytes used to represent that string. With user-defined types, LenB returns the in-memory size, including any padding between elements. For sample code that uses LenB, see the second example in the example topic.
Note Len may not be able to determine the actual number of storage bytes required when used with variable-length strings in user-defineddata types.
I mean.... it doesn't even work on files on your computer.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 27th, 2002, 08:46 AM
#11
Hyperactive Member
Originally posted by Mc Brain
Anyway, Opening the file (for input) and trapping the error always worked for me.
File not found is error 53, btw
-
Aug 27th, 2002, 09:52 AM
#12
I wonder how many charact
I mean.... it doesn't even work on files on your computer.
I think perhaps he didn't mean Len(File), but LOF(File)....
As in ... LOF(1)...
Either way, I have always used the API method, you stick in a module, and call it when you need it... if it the API method is good enough for Windows, its good enough for you.... go with API.
-
Aug 27th, 2002, 10:53 AM
#13
Software Eng.
If you don't need it to work over a network, then alex_read's method is the easiest.
-
Aug 27th, 2002, 11:28 AM
#14
Fanatic Member
looking quickly at the replies, i didn't see this api... i like to use this method in my projects:
VB Code:
Public Declare Function PathFileExists Lib "shlwapi.dll"
Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Public Function FileExists(strPath As String) As Boolean
If PathFileExists(strPath) = 1 Then
FileExists = True
Else
FileExists = False
End If
End Function
That's an easy way, and it too works on a network...
-
Aug 27th, 2002, 11:47 AM
#15
Need-a-life Member
Originally posted by nemaroller
I think perhaps he didn't mean Len(File), but LOF(File)....
As in ... LOF(1)...
Either way, I have always used the API method, you stick in a module, and call it when you need it... if it the API method is good enough for Windows, its good enough for you.... go with API.
I thought so as well... but LOF needs the file to be opened. So, if the Open statement didn't raise any error, there's no need to check the lenght of that file.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 27th, 2002, 11:56 AM
#16
Need-a-life Member
Originally posted by Megatron
If you don't need it to work over a network, then alex_read's method is the easiest.
Anyway.. I've found that sometimes the "Dir way" locks the file in same way. For example, if you check if a file exist (with this code) and then you try to delete it.... you will not be able to do it.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 27th, 2002, 01:15 PM
#17
The picture isn't missing
and i think it's FileLen(), which returns the file size in bytes. not LOF() and Len()
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
Dec 13th, 2002, 02:57 PM
#18
PowerPoster
Originally posted by Redth
looking quickly at the replies, i didn't see this api... i like to use this method in my projects:
VB Code:
Public Declare Function PathFileExists Lib "shlwapi.dll"
Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Public Function FileExists(strPath As String) As Boolean
If PathFileExists(strPath) = 1 Then
FileExists = True
Else
FileExists = False
End If
End Function
That's an easy way, and it too works on a network...
Perfect code!
-
Dec 13th, 2002, 03:35 PM
#19
It doesn't seem to work on XP. Also the line
VB Code:
Alias "PathFileExistsA" (ByVal pszPath As String) As Long
has a problem i think...
VB Code:
Public Declare Function PathFileExists Lib "shlwapi.dll"
Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Public Function FileExists(strPath As String) As Boolean
If PathFileExists(strPath) = 1 Then
FileExists = True
Else
FileExists = False
End If
End Function
-
Dec 13th, 2002, 03:41 PM
#20
Need-a-life Member
Try:
VB Code:
Public Declare Function PathFileExists Lib "shlwapi.dll" [b]_[/b]
Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 13th, 2002, 03:43 PM
#21
You're right again McBrain.Thanks...
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
|