Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    debug code -- create directory

    the code below should check if a directory path exists and if not create it
    it is not working -- can someone help me debug?


    Code:
    <%
    mynewdirectory="d:\db\2007\04\28\10"
    
    checkdirectory(mynewdirectory)
       Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(mynewdirectory) then response.write mynewdirectory & " was created"
    Function CheckDirectory(dirPath) 
    
       
    
    
       Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    
    
    If objFSO.FolderExists(dirpath)   then
    	    CheckDirectory = True
    Else
         
            CheckDirectory = False
            
                'Temp variables created.
                Dim tmpS, prefix, tmpT 
               
                Dim tmpI , tmpJ 
                'The first 3 characters of the path are 
                '     cut off and the rest of the path is spli
                '     t into an array.
                tmpS = dirPath
                prefix = Left(tmpS, 3)
                tmpS = Mid(tmpS, 4)
                tmpA = Split(tmpS, "\")
                'Loop of the array where starting from t
                '     he first folder in the path the director
                '     ies are tested.
                'If the directory does not exist it is c
                '     reated if it already exists the loop con
                '     tinues.
                'This will verify that the entire direct
                '     ory structure is created in the proper o
                '     rder.
    
                For tmpI = 0 To UBound(tmpA)
                    tmpT = prefix
    
    
                    For tmpJ = 0 To tmpI
                        tmpT = tmpT & tmpA(tmpJ) & "\"
                       
                       ' If tmpJ > 100 Then Exit Function
                    Next 
    									
    
    response.write tmt
    if tmt<>"" then
    If Not objFSO.FolderExists(tmtT) then
    
      objFSO.CreateFolder(tmt)
    End If 
    end if
                   ' If Dir(tmpT, vbDirectory) = "" Then MkDir (tmpT)
                    'In case of unknows the loop will stop a
                    '     fter 100 times.
                    'This should a safe count as a directory
                    '     structure should never reach this length
                    '     .
                    If tmpI > 100 Then Exit Function
                Next 
    
                'Directory is checked again.
                'This is in case user did not have write
                '     permissions on any part of the directory
                '     creation process.
                If objfso.folderexists(tmt) Then CheckDirectory = True
            End If
    
    
    End Function
    
    
    %>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Well, I see a missing End If right off the bat.

    If objFSO.FolderExists(mynewdirectory) then
    response.write mynewdirectory & " was created"
    End If

    Function CheckDirectory(dirPath)

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •