-
[RESOLVED] File or Directory Exists
What is the best function to tell if a file or directory exists? I'm trying to use the Dir function but I don't know what attributes to use. I want the function to be very versatile so that it can tell if a file or directory exists no matter what attributes it has. Thank you
-
Re: File or Directory Exists
You can do it this way:
Code:
Dim FilePath as string
Set fs = CreateObject("Scripting.FileSystemObject")
FilePath = 'set your full path and filename here'
If fs.FileExists(FilePath) Then
Msgbox "The file does exist"
Else
Msgbox "File does not exist"
endIf
-
Re: File or Directory Exists
Does anyone know if there's certain times when Dir doesn't work right? Can you please give an example? Thanks
-
Re: File or Directory Exists
As far as I know it works well in all cases as long as the appropriate attributes are set. It does not work well for unicode file paths or unicode file names. Regarding attributes -- can fail to return hidden items if vbHidden attr not set. The following should always return anything in the target folder:
File:
Dir$("somePath\*.*", vbHidden Or vbReadOnly Or vbSystem or vbArchive)
Directory:
Dir$("somePath\", vbHidden Or vbReadOnly Or vbSystem or vbArchive or vbDirectory)
Maybe others may chime in with specific cases where it could fail even with the above command.
-
Re: File or Directory Exists
Quote:
Originally Posted by abazabam
What is the best function to tell if a file or directory exists? I'm trying to use the Dir function but I don't know what attributes to use. I want the function to be very versatile so that it can tell if a file or directory exists no matter what attributes it has. Thank you
Use the attributes vbSystem + vbHidden. That will return any type of file or directory.
-
Re: File or Directory Exists
Quote:
Originally Posted by LaVolpe
[Snip]
Dir$("somePath\*.*", vbHidden Or vbReadOnly Or vbSystem or vbArchive)
Maybe others may chime in with specific cases where it could fail even with the above command.
I don't think you need the vbReadOnly or the vbArchive flags -- as far as I can tell they don't have any effect on the Dir$ function. I could be wrong though.
-
Re: File or Directory Exists
You may very well be correct; probably old habits talking, on my part.
-
Re: File or Directory Exists
Also, does anyone know how to read or get the file size of a file with bad characters in it? The bad characters are replaced with a question mark in VB.
-
Re: File or Directory Exists
Thats not bad characters, its a unicode character.
Also, another way to check a file exists - http://vbforums.com/showthread.php?t=349990
-
Re: File or Directory Exists
Well, how can I check its file size? When I try, it gives me an error that says something like "Bad filename or number"
-
Re: File or Directory Exists
What code are you using to check filesize?
-
Re: File or Directory Exists
Why not just use FileLen(yourfile) to get its size... also another way to check existance of a file:
Function FileExists(FileName As String) As Boolean
On Error Resume Next
' get the attributes and ensure that it isn't a directory
FileExists = (GetAttr(FileName) And vbDirectory) = 0
End Function
If FileExists(YourFile) then
msgbox "Exists"
Else
msgbox "Does not Exist"
End If
-
Re: File or Directory Exists
Quote:
Originally Posted by RobDog888
What code are you using to check filesize?
I'm using FileLen. I'm guessing I'm getting the error because the filename with "bad characters" are replaced with question marks in VB and question marks can't be used in filenames.
-
Re: File or Directory Exists
As I was saying, there are not questionmarks but rather unsupported unicode characters. You are reading the file with the wrong encoding standard or a standard that doesnt support unicode characters.
-
Re: File or Directory Exists
Another very important question. How are you getting the filename? If using commondialog control, it will not return unicode strings.
If your app is to support unicode paths/filenames, you will need to move to APIs to display a unicode-friendly open/browse dialog window, and also to open/create files and retrieve file attributes.
-
Re: File or Directory Exists
I'm actually searching for files using FindFirstFile and FindNextFile.
-
Re: File or Directory Exists
Same issue. Those have an ANSI (FindFirstFileA) version & unicode version (FindFirstFileW). The ANSI version won't return unicode text.
-
Re: File or Directory Exists
Okay, thanks, I'll try it.
-
Re: File or Directory Exists
Should I change all of the APIs' "A" to a "W"?
-
Re: File or Directory Exists
Change to the "W" so you can enable the unicode support.
-
Re: File or Directory Exists
Another way
Code:
Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
Public Function FolderExists(ByVal pstrFolder As String) As Boolean
FolderExists = (PathFileExists(pstrFolder) = 1)
If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
End Function
Public Function FileExists(ByVal pstrFile As String) As Boolean
FileExists = (PathFileExists(pstrFile) = 1)
If FileExists Then FileExists = Not (PathIsDirectory(pstrFile) <> 0)
End Function
:wave:
-
Re: File or Directory Exists
Quote:
Originally Posted by RobDog888
Change to the "W" so you can enable the unicode support.
Are there any disadvantages?
-
Re: File or Directory Exists
Possibly but none that I know of.
-
Re: File or Directory Exists
Actually, I can't get it to work anymore since I changed them to "W"s. Was I supposed to do something else like StrConv?
-
Re: File or Directory Exists
Using unicode apis are not always that easy. Most parameters in the API that are string need to be handled differently; two options (examples):
1) Pass the string parameter like: StrConv()
== FindFirstFileW(StrConv(sCriteria, vbUnicode), wfd)
2) Change API param type from Byval String to ByVal Long and pass the string parameter as StrPtr()
== FindFirstFileW(StrPtr(sCriteria), wfd)
Now when you get your FIND DATA structure back, you probably were looking for vbNullChar in the cFileName member, now you will look for vbNullChar & vbNullChar and use StrConv(wfd.cFileName, vbFromUnicode) if the filename is needed.
-
Re: File or Directory Exists