|
-
Aug 31st, 2001, 10:07 AM
#1
Thread Starter
Lively Member
testing a path
Can anyone tell me how I can test to make sure a directory exists?
Thank You
Don't ever Ginop before you Ginip 
-
Aug 31st, 2001, 10:09 AM
#2
Fanatic Member
VB Code:
Private Sub Command1_Click()
If Dir("c:\temp", vbDirectory) <> "" Then
MsgBox "Directory exists"
End If
End Sub
-
Aug 31st, 2001, 10:11 AM
#3
Addicted Member
Create a new FileSystemObject then use myFSO.FolderExists
Straight from the MSDN, so I haven't tested it.
The answer is 42. Sadly, you can't typecast the "answer" data type to the "question" data type.
Code:
Take.Me( To( Your.Leader ) )
-
Aug 31st, 2001, 10:36 AM
#4
Frenzied Member
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):
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
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.
Last edited by nishantp; Aug 31st, 2001 at 10:48 AM.
You just proved that sig advertisements work.
-
Aug 31st, 2001, 11:16 AM
#5
Fanatic Member
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.
-
Aug 31st, 2001, 11:23 AM
#6
-
Aug 31st, 2001, 03:15 PM
#7
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 ) ...
Code:
If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
Msgbox "Yep, it's here alright !"
End If
So there ! pphhhtttt
-
Aug 31st, 2001, 03:43 PM
#8
Member
Originally posted by Skitchen8
agreed
Read my signature.
-
Aug 31st, 2001, 04:10 PM
#9
Last edited by NotLKH; Aug 31st, 2001 at 04:14 PM.
-
Aug 31st, 2001, 04:11 PM
#10
Member
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.
-
Aug 31st, 2001, 04:40 PM
#11
Fanatic Member
Thats 145kb too much for my liking 
Laterz
Digital-X-Treme
Contact me on MSN Messenger: [email protected]
[VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
/ (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]
-
Aug 31st, 2001, 05:00 PM
#12
Frenzied Member
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 ) ...
Code:
If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
Msgbox "Yep, it's here alright !"
End If
So there ! pphhhtttt
I c. I suppose that would be cause a numberical comparison as faster than a string comparison. Ill do some tests.
You just proved that sig advertisements work.
-
Aug 31st, 2001, 05:12 PM
#13
Frenzied Member
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:
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
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.
Last edited by nishantp; Aug 31st, 2001 at 05:15 PM.
You just proved that sig advertisements work.
-
Sep 1st, 2001, 11:49 AM
#14
Originally posted by nishantp
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
It would seem that the "\| isnt necessary at the end of the dir path. Could someone with Win9x please test this also? ....
? I would like to once and for all optomize this function.
Win 9X Reporting in, SIR!
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
-
Sep 1st, 2001, 12:14 PM
#15
Fanatic Member
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
-
Sep 1st, 2001, 12:30 PM
#16
Fanatic Member
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]
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
-
Sep 1st, 2001, 12:36 PM
#17
Fanatic Member
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 ???
-
Sep 1st, 2001, 12:52 PM
#18
Fanatic Member
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
-
Sep 1st, 2001, 01:24 PM
#19
Frenzied Member
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
Were not actually religious fanatics you know BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.
You just proved that sig advertisements work.
-
Sep 1st, 2001, 01:37 PM
#20
Fanatic Member
Originally posted by nishantp
Were not actually religious fanatics you know BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.
OK..granted on the religious part. You guys are not as nutz as the bible-thumpers. Your still nutz....just not as bad. <g>
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
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
|