R
rebelscum0000
Dear All,
Is not working as I want
In MY code I had this lines:
'Checks if the Folder exists, If NOT THEN create it
If Len(Dir("C:\Program Files\Dups\Batch Files", vbDirectory)) = 0 Then
'will return 0
'Folder Does Not Exist
'Create Folder
MkDir ("C:\Program Files\Dups\Batch Files")
Else
Folder Exists
End If
But Dir have Lots of potential pitfalls, So I decided to try another
way
I did a few modifications in Randy's Code:
http://vbnet.mvps.org/code/fileapi/folderexists.htm
for my project but without any luck
I inserted a new module and I did call it API Folder Exists, cut and
paste the Randy's Code, At this moment I not using Forms so I deleted
that part, also I modified this part of his code;
Sub FolderExists(sFolder As String)
Dim FolderExists As Boolean
Dim hFile As Long
Dim WFD As WIN32_FIND_DATA
'remove training slash before verifying
sFolder = UnQualifyPath(sFolder)
MsgBox "sFolder = " & sFolder
'call the API pasing the folder
hFile = FindFirstFile(sFolder, WFD)
MsgBox "hFile = " & hFile
'if a valid file handle was returned,
'and the directory attribute is set
'the folder exists
FolderExists = (hFile <> INVALID_HANDLE_VALUE) And _
(WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
MsgBox "FolderExists = " & FolderExists
'clean up
Call FindClose(hFile)
End Sub
When I am call his code from MY module
Call FolderExists("C:\DVD APPZ\Microsoft\Acrobat Reader123")
sFolder returns the path and file that I do not have in my HD
hFile = -1
FolderExists = False
The code does its job, but I can not work with the Returned Results of
Randy's Code in MY module
What I am doing wrong? or How can I adapt the Randy's Code in order to
work with MY code ?
Also, I found other similar code:
Folks with a religious aversion to Error trapping <g>, can drop in the
API
equivalent... ->(I AM ONE Of THIS KIND LOL)
Private Declare Function GetFileAttributes Lib "kernel32" Alias
"GetFileAttributesA" (ByVal lpFileName As String) As Long Private
Const INVALID_FILE_ATTRIBUTES As Long = -1&
Private Const ERROR_SHARING_VIOLATION As Long = 32&
Public Function FileExists(ByVal FileName As String) As Boolean
Dim nAttr As Long
' Grab this files attributes, and make sure it isn't a folder.
' This test includes cases where file doesn't exist at all.
nAttr = GetFileAttributes(FileName)
If (nAttr And vbDirectory) <> vbDirectory Then
FileExists = True
ElseIf Err.LastDllError = ERROR_SHARING_VIOLATION Then
FileExists = True
End If
End Function
Public Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
' Grab the attributes, test valid values for folder bit.
nAttr = GetFileAttributes(PathName)
If nAttr <> INVALID_FILE_ATTRIBUTES Then
If (nAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
End If
End Function
But I think is for Visual Net or something, It turns red the first
line of the code when I paste into My Module, also I do know how to
call it or use it
Finally if this is not possible and someone can point me to ANOTHER
similar code it will be OK
Thanks in advance for any idea, suggestion or help
Regards,
Antonio Macias
Is not working as I want
In MY code I had this lines:
'Checks if the Folder exists, If NOT THEN create it
If Len(Dir("C:\Program Files\Dups\Batch Files", vbDirectory)) = 0 Then
'will return 0
'Folder Does Not Exist
'Create Folder
MkDir ("C:\Program Files\Dups\Batch Files")
Else
Folder Exists
End If
But Dir have Lots of potential pitfalls, So I decided to try another
way
I did a few modifications in Randy's Code:
http://vbnet.mvps.org/code/fileapi/folderexists.htm
for my project but without any luck
I inserted a new module and I did call it API Folder Exists, cut and
paste the Randy's Code, At this moment I not using Forms so I deleted
that part, also I modified this part of his code;
Sub FolderExists(sFolder As String)
Dim FolderExists As Boolean
Dim hFile As Long
Dim WFD As WIN32_FIND_DATA
'remove training slash before verifying
sFolder = UnQualifyPath(sFolder)
MsgBox "sFolder = " & sFolder
'call the API pasing the folder
hFile = FindFirstFile(sFolder, WFD)
MsgBox "hFile = " & hFile
'if a valid file handle was returned,
'and the directory attribute is set
'the folder exists
FolderExists = (hFile <> INVALID_HANDLE_VALUE) And _
(WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
MsgBox "FolderExists = " & FolderExists
'clean up
Call FindClose(hFile)
End Sub
When I am call his code from MY module
Call FolderExists("C:\DVD APPZ\Microsoft\Acrobat Reader123")
sFolder returns the path and file that I do not have in my HD
hFile = -1
FolderExists = False
The code does its job, but I can not work with the Returned Results of
Randy's Code in MY module
What I am doing wrong? or How can I adapt the Randy's Code in order to
work with MY code ?
Also, I found other similar code:
Folks with a religious aversion to Error trapping <g>, can drop in the
API
equivalent... ->(I AM ONE Of THIS KIND LOL)
Private Declare Function GetFileAttributes Lib "kernel32" Alias
"GetFileAttributesA" (ByVal lpFileName As String) As Long Private
Const INVALID_FILE_ATTRIBUTES As Long = -1&
Private Const ERROR_SHARING_VIOLATION As Long = 32&
Public Function FileExists(ByVal FileName As String) As Boolean
Dim nAttr As Long
' Grab this files attributes, and make sure it isn't a folder.
' This test includes cases where file doesn't exist at all.
nAttr = GetFileAttributes(FileName)
If (nAttr And vbDirectory) <> vbDirectory Then
FileExists = True
ElseIf Err.LastDllError = ERROR_SHARING_VIOLATION Then
FileExists = True
End If
End Function
Public Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
' Grab the attributes, test valid values for folder bit.
nAttr = GetFileAttributes(PathName)
If nAttr <> INVALID_FILE_ATTRIBUTES Then
If (nAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
End If
End Function
But I think is for Visual Net or something, It turns red the first
line of the code when I paste into My Module, also I do know how to
call it or use it
Finally if this is not possible and someone can point me to ANOTHER
similar code it will be OK
Thanks in advance for any idea, suggestion or help
Regards,
Antonio Macias