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.
Printable View
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.
Figure it out yourself... the only way to learn.Quote:
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.
VB Code:
If Not (Len(Dir$("C:\boot.ini")) <> 0) Then MsgBox "File Doesn't Exist!" Else MsgBox "File Does Exist" End If
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
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?Quote:
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.
Anyway, Opening the file (for input) and trapping the error always worked for me.
I don't understand what he tried to write... but, as it's written, that doesn't work at all.Quote:
Originally posted by James Stanich
Will this work for a network?
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.Quote:
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 mean.... it doesn't even work on files on your computer.Quote:
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.
File not found is error 53, btwQuote:
Originally posted by Mc Brain
Anyway, Opening the file (for input) and trapping the error always worked for me.
I think perhaps he didn't mean Len(File), but LOF(File)....Quote:
I mean.... it doesn't even work on files on your computer.
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.
If you don't need it to work over a network, then alex_read's method is the easiest.
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... :)
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.Quote:
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.
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.Quote:
Originally posted by Megatron
If you don't need it to work over a network, then alex_read's method is the easiest.
and i think it's FileLen(), which returns the file size in bytes. not LOF() and Len() :p
Perfect code!Quote:
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... :)
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
Try:
VB Code:
Public Declare Function PathFileExists Lib "shlwapi.dll" [b]_[/b] Alias "PathFileExistsA" (ByVal pszPath As String) As Long
You're right again McBrain.Thanks...