Smart Folder Creation

Author Message
ebgreen

  • Total Posts : 8227
  • Scores: 98
  • Reward points : 0
  • Joined: 7/12/2005
  • Status: offline
Smart Folder Creation Friday, December 23, 2005 5:43 AM (permalink)
5
This Sub will create a folder and the entire path to the folder if need be.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ''    
  ''    Sub SmartCreateFolder(strFolder)
  ''    Author:        ebgreen
  ''    Purpose:    Creates a folder building the entire path if needed.
  ''    Takes:        strFolder - The complete path of the folder to create
  ''    Returns:    Nothing
  ''    Revision History:
  ''        12/23/05    RMD    Created
  ''
  ''    To Do:
  ''        Nothing at present
  ''
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Sub SmartCreateFolder(strFolder)
      Dim oFSO:Set oFSO = CreateObject("Scripting.FileSystemObject")
      If oFSO.FolderExists(strFolder) Then
          Exit Sub
      Else
          SmartCreateFolder(oFSO.GetParentFolderName(strFolder))
      End If
      oFSO.CreateFolder(strFolder)
      Set oFSO = Nothing    
  End Sub
  

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm
 
#1
    DiGiTAL.SkReAM

    • Total Posts : 1259
    • Scores: 7
    • Reward points : 0
    • Joined: 9/7/2005
    • Location: Clearwater, FL, USA
    • Status: offline
    RE: Smart Folder Creation Wednesday, March 01, 2006 9:26 AM (permalink)
    0
    AAARRRGGGHHH!!!!!
    Why didn't i take a look thru here earlier?!?  It would have saved me from havign to create this monstrosity to do the same thing!
     
    *quietly wanders off towards the gunrack*
     
     Function fCreateFolder(sFolderPathToCreate)
     ' This function checks for the existence of a folder.  If it does not exist,
     ' it will create the folder and any parent folders that do not exist, as well.  
     ' It can handle either UNC paths or drive letter paths.
     ' The function will return an error code of 0 if no errors were encountered,
     ' and a 1 if any errors were encountered.
      Dim iStartCharacterPosition, sFolderPathPrefix, sFolderPathSuffix, aFolders, iElement, sFolderName, iError, iResult
       If Trim(sFolderPathToCreate) = "" Or IsNull(sFolderPathToCreate) or len(sFolderPathToCreate) < 4 Then
        iResult = 1
       Else 
         If Not oFSO.FolderExists(sFolderPathToCreate) Then 
           Select Case Left(Trim(sFolderPathToCreate), 2)
            Case "\\"
             iStartCharacterPosition = InStr(3, sFolderPathToCreate, "\")
             sFolderPathPrefix = Left(sFolderPathToCreate, iStartCharacterPosition)
            Case Else
             iStartCharacterPosition = 3
             sFolderPathPrefix = Left(sFolderPathToCreate, 3)
           End Select
           If Len(sFolderPathToCreate) > Len(sFolderPathPrefix) Then 
            sFolderPathSuffix = Right(sFolderPathToCreate, Len(sFolderPathToCreate) - iStartCharacterPosition)
            iResult = 0
           Else
            iResult = 1
           End If 
           Select Case iResult
            Case 0
             aFolders = Split(sFolderPathSuffix, "\")
              For iElement = 0 to UBound(aFolders)
               sFolderName = sFolderName & aFolders(iElement) & "\"
                If Not oFSO.FolderExists(sFolderPathPrefix & sFolderName) And iElement > 0 Then
                 On Error Resume Next
                  oFSO.CreateFolder(sFolderPathPrefix & sFolderName)
                  iError = Err.Number
                 On Error GoTo 0
                  Select Case iError
                   Case 0
                    iResult = 0
                   Case Else
                    iResult = 1
                  End Select 
                End If 
              Next
            Case Else 
             iResult = 1
           End Select 
         Else
          iResult = 0
         End If 
       End If
     fCreateFolder = iResult
     End Function
     

    "Would you like to touch my monkey?" - Dieter (Mike Meyers)

    "It is better to die like a tiger, than to live like a pussy."
    -Master Wong, from Balls of Fury
     
    #2
      ebgreen

      • Total Posts : 8227
      • Scores: 98
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      RE: Smart Folder Creation Wednesday, March 01, 2006 10:22 AM (permalink)
      0
      Recursion is your friend. 
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        robwm

        • Total Posts : 118
        • Scores: 0
        • Reward points : 0
        • Joined: 3/10/2009
        • Location: Seattle, WA
        • Status: offline
        Re:Smart Folder Creation Friday, December 09, 2011 8:36 AM (permalink)
        0
        ebgreen,
         
        Could you explain how this works? I can't quite wrap my head around it. It does work great and it's so compact that you deserve a lot more than 5 stars!
         
        Thanks,
        Rob
         
        #4
          59cobalt

          • Total Posts : 981
          • Scores: 91
          • Reward points : 0
          • Joined: 7/17/2011
          • Status: offline
          Re:Smart Folder Creation Saturday, December 10, 2011 12:52 AM (permalink)
          5
          The trick is to check if the parent folder already exists, and otherwise create that folder first, before creating the child folder. Using recursion you traverse the path "upwards" until you reach the bottom-most already existing folder, and then create the missing child folders descending back "downwards" in the path. The only precondition for calling the procedure is that the drive must exist, because the recursion is based on the assumption that at least the root folder of the drive exists.

          You could simplify the procedure even more by checking if the folder does not exist, because then the procedure will automatically return without doing anything when a folder already exists:
          Sub SmartCreateFolder(strFolder)
           Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
           If Not oFSO.FolderExists(strFolder) Then
           SmartCreateFolder oFSO.GetParentFolderName(strFolder)
           oFSO.CreateFolder(strFolder)
           End If
          End Sub

           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9