Topic: This API is Cool! Randy Birch FolderExists Routine but..

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
 
D

Douglas J. Steele

Subs don't return values: only functions do. That's why Randy had it as a
function.

Change it back to what Randy had.

Function FolderExists(sFolder As String) As Boolean
Dim FolderExists As Boolean
Dim hFile As Long
Dim WFD As WIN32_FIND_DATA

'remove training slash before verifying
sFolder = UnQualifyPath(sFolder)
'call the API pasing the folder
hFile = FindFirstFile(sFolder, WFD)
'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)
'clean up
Call FindClose(hFile)

End Function

You'd then use

If FolderExists("C:\DVD APPZ\Microsoft\Acrobat Reader123") Then
' folder exists
Else
' folder does not exist
End If
 
R

rebelscum0000

Change it back to what Randy had.

Thank you, Now Woks and it is still Cool without the "but.." LOL

.......And with this API Can I also find out :

Type : File Folder
Location : C:\HP PMD P\10-01-06
Size : 23.2 GB (24,973,459,400 bytes)
Size on disk : 23.2 GB (24,973,467,648 bytes)
Contains: 3 Files, 1 Folder
Created : Tuesday, October 24, 2006, 2:18:00 PM
Attributs : Read Only (IF is Checked OR NOT) or Hidden (IF is Checked
OR NOT)

How?

Or I will need to work with another code?

FindFirstFile: An API 'FolderExists' Routine by Randy Birch

->This function will return the correct result regardless of any
additional attributes assigned to the directory.

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Thanks in advance
Regards,
Antonio Macias
 
D

Douglas J. Steele

You can get CreationTime, LastAccessTime and LastWriteTime by looking at the
value returned in WFD.ftCreationTime, WFD.ftLastAccessTime and
WFD.ftLastWriteTime (You'll have to use the FileTimeToSystemTime API call to
convert from the FILETIME structure to something you can use, as illustrated
in http://vbnet.mvps.org/code/fileapi/filedatetime.htm)

To get the folder attributes, you can AND whatever's returned in
WDF.dwFileAttributes with the appropriate FILE_ATTRIBUTE_ constants.

Size can't be obtained through a API call: you have to loop through the
folder and calculate it for yourself (or you can use FSO if you insist)
 
R

rebelscum0000

Douglas,

I know you are going to kill me... but before fight with
http://vbnet.mvps.org/code/fileapi/filedatetime.htm LOL......

Now I want to replace the MkDir for an API also by Randy Birch's
CreateDirectory: Creating Nested Folders
http://vbnet.mvps.org/index.html?code/file/nested.htm

So, I did the same: inserted a new module and I named API
CreateDirectory Nested Folders, I did cut and paste the Randy's
Code....... OH! This time I did not changed the original code

'Initialize New Event API_Folder_Exists
'API Folder Exists
'Checks IF the Folder exists, IF NOT THEN create it
If FolderExists("C:\Program Files\Dups\Batch Files1") = True Then
' folder exists
MsgBox "No Problem, I need some rest"
Else
' folder does not exist
API CreateDirectory: Creating Nested Folders
MsgBox "Why I do not understand how to use API in my Modules?"
End IF

As I said: At this moment I am not USING Forms, (Of course I tested
first with a form, once it noticed how it worked I deleted the form)

Same Problem I do not know how to call the Randy's API from MY MODULE

I have tested all the ways but without any luck,

'buff = "c:\DemoByPath\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"
buff = "c:\DemoByPath
'CreateNestedFoldersByPath
'completeDirectory ("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8")
'CreateNestedFoldersByPath ("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8")
'CreateNestedFoldersByPath ("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8")
'Call completeDirectory("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8")
'CreateDirectory (completeDirectory)
'If CreateDirectory("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8") = 1 Then
'MsgBox "Failed"
'Else
'MsgBox "success"
'End If
'newDirectory = "c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"
'r = CreateDirectory(newDirectory, 0)
'newDirectory = "c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"
'r = CreateDirectory(newDirectory, 0)
'nMade = CreateNestedFoldersByPath(buff)
'r = CreateDirectory(buff)
'd = completeDirectory("c:\DemoByPath")
'CreateDirectory = "c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"
'CreateDirectory
'r = "c:\DemoByPath\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"
'Do not create dirs
'If CreateDirectory = 1 Then
'CreateNestedFoldersByPath ("c:\DemoByPath
\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8")
'End If
'buff = "c:\Demo\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7\Sub8"

Now what I am doing wrong? or How can I adapt the Randy's Code in
order to
work with MY MODULE ?

Thanks in advance

Regards,
Antonio Macias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top