|
-
Dec 19th, 2007, 08:54 PM
#1
Thread Starter
Hyperactive Member
[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
-
Dec 19th, 2007, 09:26 PM
#2
Hyperactive Member
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
-
Dec 19th, 2007, 09:41 PM
#3
Thread Starter
Hyperactive Member
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
-
Dec 19th, 2007, 09:46 PM
#4
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.
Last edited by LaVolpe; Dec 19th, 2007 at 09:49 PM.
-
Dec 19th, 2007, 09:47 PM
#5
Re: File or Directory Exists
 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.
-
Dec 19th, 2007, 09:50 PM
#6
Re: File or Directory Exists
 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.
-
Dec 19th, 2007, 09:52 PM
#7
Re: File or Directory Exists
You may very well be correct; probably old habits talking, on my part.
-
Dec 20th, 2007, 02:59 PM
#8
Thread Starter
Hyperactive Member
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.
-
Dec 20th, 2007, 03:03 PM
#9
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 20th, 2007, 03:12 PM
#10
Thread Starter
Hyperactive Member
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"
-
Dec 20th, 2007, 03:58 PM
#11
Re: File or Directory Exists
What code are you using to check filesize?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 20th, 2007, 06:32 PM
#12
Hyperactive Member
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
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
-
Dec 21st, 2007, 12:59 PM
#13
Thread Starter
Hyperactive Member
Re: File or Directory Exists
 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.
-
Dec 21st, 2007, 01:03 PM
#14
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 21st, 2007, 01:09 PM
#15
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.
-
Dec 21st, 2007, 01:34 PM
#16
Thread Starter
Hyperactive Member
Re: File or Directory Exists
I'm actually searching for files using FindFirstFile and FindNextFile.
-
Dec 21st, 2007, 01:48 PM
#17
Re: File or Directory Exists
Same issue. Those have an ANSI (FindFirstFileA) version & unicode version (FindFirstFileW). The ANSI version won't return unicode text.
-
Dec 21st, 2007, 01:50 PM
#18
Thread Starter
Hyperactive Member
Re: File or Directory Exists
Okay, thanks, I'll try it.
-
Dec 21st, 2007, 02:05 PM
#19
Thread Starter
Hyperactive Member
Re: File or Directory Exists
Should I change all of the APIs' "A" to a "W"?
-
Dec 21st, 2007, 02:15 PM
#20
Re: File or Directory Exists
Change to the "W" so you can enable the unicode support.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 21st, 2007, 02:19 PM
#21
Frenzied Member
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
-
Dec 21st, 2007, 02:31 PM
#22
Thread Starter
Hyperactive Member
Re: File or Directory Exists
 Originally Posted by RobDog888
Change to the "W" so you can enable the unicode support.
Are there any disadvantages?
-
Dec 21st, 2007, 03:58 PM
#23
Re: File or Directory Exists
Possibly but none that I know of.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 21st, 2007, 04:01 PM
#24
Thread Starter
Hyperactive Member
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?
-
Dec 21st, 2007, 04:07 PM
#25
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.
Last edited by LaVolpe; Dec 21st, 2007 at 07:06 PM.
-
Jan 2nd, 2008, 02:15 AM
#26
Thread Starter
Hyperactive Member
Re: File or Directory Exists
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
|