Create Folder Tree 
Author Message
 Create Folder Tree

VB6SP6 Back again with question
I need to create a folder tree.
It can originate as
   C:\Path1\Path2\Path3
or
  \\Path1\Path2\Path3
The routine needs to be able to create any portion that does not already
exists.
MkDir or Dir does not seem to support the \\Path1\
I have a routine that uses Mkir and Dir, but only seems to work for non
\\Path\

' ====================
I ran across
Private Declare Function MakeSureDirectoryPathExists Lib "IMAGEHLP.DLL" _
(ByVal DirPath As String) As Long

    bResult = MakeSureDirectoryPathExists(sPathIn)

but I am not sure if that is appropriate since what is IMAGEHLP.DLL?

' =======================
I tried working with

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lSecurityDescriptor As Long
    bInheritHandle As Long
End Type
'
Private Declare Function CreateDirectory Lib "kernel32" Alias
"CreateDirectoryA" _
    (ByVal lpPathName As String, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
'

Private Function CreateFolder(sFolder As String) As Boolean

    On Error GoTo CreateFolderErr

    Dim tSA As SECURITY_ATTRIBUTES
    Dim lRet As Long

    tSA.nLength = LenB(tSA)

    ' If the function succeeds, the return value is nonzero.
    lRet = CreateDirectory(sFolder, tSA)   ' returns zero on failure

    CreateFolder = (lRet <> 0) ' true on success

CreateFolderExit:
    Exit Function

CreateFolderErr:
    Resume CreateFolderExit

End Function 'CreateFolder

Not sure how to deal with this since first I need to know how to check if
partial path is there.  What do I use to check if partial path exists?



Tue, 14 Dec 2010 02:39:02 GMT  
 Create Folder Tree

Quote:

> VB6SP6 Back again with question
> I need to create a folder tree.
> It can originate as
>   C:\Path1\Path2\Path3
> or
>  \\Path1\Path2\Path3
> The routine needs to be able to create any portion that does not already
> exists.
> MkDir or Dir does not seem to support the \\Path1\
> I have a routine that uses Mkir and Dir, but only seems to work for non
> \\Path\

This seems to work pretty well:

   ' http://msdn.microsoft.com/en-us/library/bb762131(VS.85).aspx
   Private Declare Function SHCreateDirectoryExW Lib "shell32" (ByVal hWnd As Long,
ByVal lpszPath As Long, ByVal lpSA As Long) As Long

   Public Function PathMakeDirs(ByVal Path As String) As Boolean
      Dim nRet As Long
      ' Potential error codes.
      Const ERROR_SUCCESS As Long = 0&
      Const ERROR_FILENAME_EXCED_RANGE As Long = 206&
      Const ERROR_BAD_PATHNAME As Long = 161&
      Const ERROR_PATH_NOT_FOUND As Long = 3&
      Const ERROR_FILE_EXISTS As Long = 80&
      Const ERROR_ALREADY_EXISTS As Long = 183&
      Const ERROR_CANCELLED As Long = 1223&
      ' Requires v5.0 of Shell32.dll...
      nRet = SHCreateDirectoryExW(0&, StrPtr(Path), 0&)
      ' Treat "already exists" as success...
      PathMakeDirs = _
         (nRet = ERROR_SUCCESS) Or _
         (nRet = ERROR_FILE_EXISTS) Or _
         (nRet = ERROR_ALREADY_EXISTS)
   End Function

One thing that bothers me though, is the apparent limit on path length.

   "A pointer to a null-terminated string specifying the fully qualified path of the
directory. This string is of maximum length of 248 characters, including the
terminating null character."

That doesn't make sense because the MakeSureDirectoryPathExists docs state:

   "This function does not support Unicode strings. To specify a Unicode path, use
the SHCreateDirectoryEx function."

And Unicode paths can be *much* longer than that!  I tested it, and indeed, it fails
at that point.  Not sure what the best remedy there is.  There may indeed be no
shortcut here.
--
.NET: It's About Trust!
 http://vfred.mvps.org



Tue, 14 Dec 2010 03:29:18 GMT  
 Create Folder Tree
Thanks.  I will give this a try.
Quote:


> > VB6SP6 Back again with question
> > I need to create a folder tree.
> > It can originate as
> >   C:\Path1\Path2\Path3
> > or
> >  \\Path1\Path2\Path3
> > The routine needs to be able to create any portion that does not already
> > exists.
> > MkDir or Dir does not seem to support the \\Path1\
> > I have a routine that uses Mkir and Dir, but only seems to work for non
> > \\Path\

> This seems to work pretty well:

>    ' http://msdn.microsoft.com/en-us/library/bb762131(VS.85).aspx
>    Private Declare Function SHCreateDirectoryExW Lib "shell32" (ByVal hWnd As Long,
> ByVal lpszPath As Long, ByVal lpSA As Long) As Long

>    Public Function PathMakeDirs(ByVal Path As String) As Boolean
>       Dim nRet As Long
>       ' Potential error codes.
>       Const ERROR_SUCCESS As Long = 0&
>       Const ERROR_FILENAME_EXCED_RANGE As Long = 206&
>       Const ERROR_BAD_PATHNAME As Long = 161&
>       Const ERROR_PATH_NOT_FOUND As Long = 3&
>       Const ERROR_FILE_EXISTS As Long = 80&
>       Const ERROR_ALREADY_EXISTS As Long = 183&
>       Const ERROR_CANCELLED As Long = 1223&
>       ' Requires v5.0 of Shell32.dll...
>       nRet = SHCreateDirectoryExW(0&, StrPtr(Path), 0&)
>       ' Treat "already exists" as success...
>       PathMakeDirs = _
>          (nRet = ERROR_SUCCESS) Or _
>          (nRet = ERROR_FILE_EXISTS) Or _
>          (nRet = ERROR_ALREADY_EXISTS)
>    End Function

> One thing that bothers me though, is the apparent limit on path length.

>    "A pointer to a null-terminated string specifying the fully qualified path of the
> directory. This string is of maximum length of 248 characters, including the
> terminating null character."

> That doesn't make sense because the MakeSureDirectoryPathExists docs state:

>    "This function does not support Unicode strings. To specify a Unicode path, use
> the SHCreateDirectoryEx function."

> And Unicode paths can be *much* longer than that!  I tested it, and indeed, it fails
> at that point.  Not sure what the best remedy there is.  There may indeed be no
> shortcut here.
> --
> ..NET: It's About Trust!
>  http://vfred.mvps.org



Tue, 14 Dec 2010 04:05:08 GMT  
 Create Folder Tree

Quote:

> Thanks.  I will give this a try.

Okay, but be aware that it will require machines with IE5+ installed.
--
.NET: It's About Trust!
 http://vfred.mvps.org


Tue, 14 Dec 2010 04:17:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Creating Folder and Tasks in Folder

2. Programmatically creating multiple folders in a folder

3. Creating folders under Personal Folders

4. create a folder or find default temp folder

5. Create new folder in current folder in explorer window

6. folder browsing - create folder problem

7. Check for a Folder or Create a New Folder from within vb

8. Recurively Iterate through folder tree?

9. VBA add other users mailboxes to the outlook explorer / folder tree

10. Recurively Iterate through folder tree?

11. File Explorer in a Tree View box, but displaying only one folder

12. tree structure (expanding collapsing explorer folder style)

 

 
Powered by phpBB® Forum Software