Can anyone tell me how I can test to make sure a directory exists?
Thank You
Printable View
Can anyone tell me how I can test to make sure a directory exists?
Thank You
VB Code:
Private Sub Command1_Click() If Dir("c:\temp", vbDirectory) <> "" Then MsgBox "Directory exists" End If End Sub
Create a new FileSystemObject then use myFSO.FolderExists
Straight from the MSDN, so I haven't tested it.
You gotta be kidding me. You cant be asking him to add 255K to his project for the FSO object just to check if a dir exists. Heres a function i wrote (after much debate):
The on error resume next is there cause on WinNT and 2K, an error occurs if you point the function to a file. Not required in Win9x.If you want the whole darn debate, here it is.VB Code:
Function DirExist(ByVal DirPath As String) As Boolean On Error Resume Next 'The above line is only required on Win NT4, and 2K. Remove on Win9x If Right$(DirPath, 1) <> "\" Then DirPath = DirPath & "\" DirExist = Dir$(DirPath, vbDirectory) <> "" End Function
255k? where did you get your dll? it's only 145k or so. and not that bad. both work, keep negative comments like that out of it. Alternate methods, that's all there is to it.
OH its only 145K? Sorry. Anyways the code in that function is 269 bytes!..so beat that!:D And thats with the comments!:p
FSO = Evil:D
Yeah, not bad - nishantp's is the best attempt so far, this is the fastest way (as was proved by some gurus on this site ages ago with nothing better to do than test this :D ) ...
So there ! pphhhtttt :pCode:If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
Msgbox "Yep, it's here alright !"
End If
Read my signature. :DQuote:
Originally posted by Skitchen8
agreed
Sorry,
I have signatures turned off right now, and am too lazy to turn them back on.
:D ;) :D ;) :D
-Lou
heh,heh:
as seen HERE:Quote:
Originally posted by filburt1
I have sigs off and I am too lazy to turn them on. :D
http://www.vbforums.com/showthread.p...threadid=95057
Arien Talabac
The 100,000th poster of Chit Chat
Member of the MSN Replacement development team
Comment on PassProg in preparation for the next major release!
These elements of VB are members of an evil cult:
- FileSystemObject
- Variant
- Global
- Anything like Form1, Text4, Command14, Image3, etc.
Thats 145kb too much for my liking :pQuote:
it's only 145kb
Laterz
I c. I suppose that would be cause a numberical comparison as faster than a string comparison. Ill do some tests.Quote:
Originally posted by alex_read
Yeah, not bad - nishantp's is the best attempt so far, this is the fastest way (as was proved by some gurus on this site ages ago with nothing better to do than test this :D ) ...
So there ! pphhhtttt :pCode:If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
Msgbox "Yep, it's here alright !"
End If
Ok i did a test involving a loop of 200 done 100 times and i took the average time. It seems that useing the Len(dir) method and the numerical comparison is slighly faster. Very slightly. My a few milliseconds. I did this test on a P450 with 64MB RAM.
Also, i have Win2K and Fat32 (yes there is a reason for that). On my machine, this works also:
It would seem that the "\| isnt necessary at the end of the dir path. Could someone with Win9x please test this also? And perhaps some people with NTFS? I would like to once and for all optomize this function.VB Code:
Function DirExist(ByVal DirPath As String) As Boolean On Error Resume Next 'The above line is only required on Win NT4, and 2K. Remove on Win9x DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0 End Function
Win 9X Reporting in, SIR! :DQuote:
Originally posted by nishantp
It would seem that the "\| isnt necessary at the end of the dir path. Could someone with Win9x please test this also? ....VB Code:
Function DirExist(ByVal DirPath As String) As Boolean On Error Resume Next 'The above line is only required on Win NT4, and 2K. Remove on Win9x DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0 End Function
? I would like to once and for all optomize this function.
Satus Report:
Works Perfectly, And for consistency, I recommend leaving
the On Error Resume Next, so as to not confuse people,
if it turns out to work everywhere.
BTW, Good Code, All!
{Just to be different, a method could be built around
the Name function {{Name DirPath As DirPath}}, and when an error is raised, that means the dir didn't exist, BUT, heh, why have
five or six lines, when 1 or 2 will do it much more quickly?}
:) :) :)
-Lou
Evil ???? Boy, I hate religious fanatics. Root of 99% of the problems in the world. The other 1% is Bill Gates fault.Quote:
Originally posted by nishantp
OH its only 145K? Sorry. Anyways the code in that function is 269 bytes!..so beat that!:D And thats with the comments!:p
FSO = Evil:D
269 bytes with a run-time dependency that STARTS at 1.35MB. Imagine that. I bet all the C++ and Delphi programmers must be shaking in their undies right now.
Following is lame but it works.
VB Code:
On Error Resume Next ChDir "c:\temp" If Err.Number <> 0 Then MsgBox "folder not found" End If
Even worse: Any one know how to keep SHFileOperation from popping up the confirmation dialog ?? I know it's a flag of some sort but can't find it.
VB Code:
'In general section Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Type SHFILEOPSTRUCT hWnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAborted As Boolean hNameMaps As Long sProgress As String End Type Private Const FO_DELETE = &H3 Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Sub Command1_Click() If FolderExist("c:\bob") = True Then MsgBox "it's there" Else MsgBox "Boo-Hoo" End If End Sub Public Function FolderExist(sFolderPath) As Boolean 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim Security As SECURITY_ATTRIBUTES 'Create a directory Ret& = CreateDirectory(sFolderPath, Security) 'If CreateDirectory returns 0, the function has failed If Ret& = 0 Then FolderExist = True Else Dim SHDirOp As SHFILEOPSTRUCT With SHDirOp .wFunc = FO_DELETE .pFrom = sFolderPath End With 'Delete the directory SHFileOperation SHDirOp FolderExist = False End If End Function
Never mind. Found the RemoveDirectory API call.
VB Code:
If Ret& = 0 Then FolderExist = True Else RemoveDirectory sFolderPath FolderExist = False End If
Hmmmm......how about a FolderExists function that scans the whole drive getting all folders looking to see if it's there ??
How in-efficent can I make this function ???
FSO to the rescue.
VB Code:
Option Explicit Private fso As FileSystemObject Private aPath() As String Private lPathCount As Long Public Sub GetTheFiles(ByVal sFolderToSearch As String) Dim fsoFolder As Folder ' get sub-folders in current folder Set fsoFolder = fso.GetFolder(sFolderToSearch) ' loop thru all sub-flders If fsoFolder.SubFolders.Count > 0 Then For Each fsoFolder In fsoFolder.SubFolders If (lPathCount Mod 500) = 0 Then ReDim Preserve aPath(0 To UBound(aPath) + 500) End If aPath(lPathCount) = fsoFolder.Path lPathCount = lPathCount + 1 ' recurse thru them Call GetTheFiles(fsoFolder.Path) DoEvents Next End If Exit Sub End Sub Public Function FolderExists(sPath) As Boolean Dim s As String Dim l As Long MousePointer = vbHourglass Call GetTheFiles(Left$(sPath, 3)) ReDim Preserve aPath(0 To lPathCount - 1) FolderExists = False For l = 0 To UBound(aPath) If UCase$(aPath(l)) = UCase$(sPath) Then FolderExists = True Exit For End If Next MousePointer = vbDefault Erase aPath End Function Private Sub Command1_Click() If FolderExists("c:\john") = True Then MsgBox "It's there" Else MsgBox "Boo-Hoo" End If End Sub Private Sub Form_Load() Set fso = New FileSystemObject ReDim aPath(0 To 500) lPathCount = 0 End Sub
Were not actually religious fanatics you know:D BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.Quote:
Originally posted by Patoooey
Evil ???? Boy, I hate religious fanatics. Root of 99% of the problems in the world. The other 1% is Bill Gates fault.
269 bytes with a run-time dependency that STARTS at 1.35MB. Imagine that. I bet all the C++ and Delphi programmers must be shaking in their undies right now.
Following is lame but it works.
VB Code:
On Error Resume Next ChDir "c:\temp" If Err.Number <> 0 Then MsgBox "folder not found" End If
OK..granted on the religious part. You guys are not as nutz as the bible-thumpers. Your still nutz....just not as bad. <g>Quote:
Originally posted by nishantp
Were not actually religious fanatics you know:D BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.
AND !!!...if you compress the FSO DLL...it's now a mere 61kb. Hell, even at 56kb that's only a few seconds at most to download. After sitting thru the "for-ever" download of the run-time, a few more seconds is no problem.
And the 1.3MB is a minimum. Never tried to see what is possible with only that one file but I bet it ain't much. Hmmm....wonder what you can do with just that one file.....Naw...I'm not screwing up my system to test it out. hehehehehe