Results 1 to 17 of 17

Thread: creating a folder in VB

  1. #1

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91

    Cool

    Hello,
    I am looking for a way to allow users to create a new folder as well as browse for an existing one and allow the user to select it... I have a few options. I would like to allow users to browse the exising folders, and have an option to create one. I have this code that allows the browsing of folders:

    Private Sub Command2_Click()
    Dim strResFolder As String
    strResFolder = BrowseForFolder(hwnd, "Please select a folder.")
    If strResFolder = "" Then
    Call MsgBox("The Cancel button was pressed.", vbExclamation)
    Else
    Call MsgBox("The folder " & strResFolder & " was selected.", vbExclamation)
    End If
    Text1 = strResFolder
    End Sub

    where strResFolder is declared in an API

    there isn't a way that I can have a button similar to the CommonDialog box button for creating a new folder?

    Or maybe I can just ask if they want to create a folder, and have them enter the name. Does anyone know of code to do that? Similar to what you see when intalling items into your computer through an installer.

    What my target is is a way to allow users to place files with as much ease an accessability as possible. A commondialog box has an option to create a folder, but, the selection of one file is NOT what I am looking for. Jsut a way for users to locate a folder or create one of thier own.

  2. #2
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463
    Look in the tips section of VBworld there is a great way to create directory structures. Or the code that Mathew just entered will also work!!!
    TMacPherson
    MIS Systems Engineer
    [email protected]


  3. #3
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463
    Never mind the tip section here is the code just add a form and a command button to the form and paste the code into the general section. This will create the Directory's C:\test\directory\vb\tips

    Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor 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



    Public Sub CreateNewDirectory(NewDirectory As String)
    Dim sDirTest As String
    Dim SecAttrib As SECURITY_ATTRIBUTES
    Dim bSuccess As Boolean
    Dim sPath As String
    Dim iCounter As Integer
    Dim sTempDir As String
    Dim iFlag
    iFlag = 0
    sPath = NewDirectory + "\x"

    If Right(sPath, Len(sPath)) <> "" Then
    sPath = sPath & ""
    End If

    iCounter = 1

    Do Until InStr(iCounter, sPath, "") = Len(sPath)
    iCounter = InStr(iCounter, sPath, "")
    sTempDir = Left(sPath, iCounter)
    sDirTest = Dir(sTempDir)
    iCounter = iCounter + 1
    If Right(sTempDir, 1) <> "\" Then GoTo Missit
    'create directory
    SecAttrib.lpSecurityDescriptor = &O0
    SecAttrib.bInheritHandle = False
    SecAttrib.nLength = Len(SecAttrib)
    bSuccess = CreateDirectory(sTempDir, SecAttrib)
    Missit:
    Loop


    End Sub


    Private Sub Command1_Click()
    Call CreateNewDirectory("C:\test\directory\vb\tips")
    End Sub
    TMacPherson
    MIS Systems Engineer
    [email protected]


  4. #4
    Guest
    Didn't think anyone saw that. But here it is again:

    Code:
    Private Sub Command1_Click()
    Dim x
    x = Inputbox("Directory to create?", "Folder:")
    If x = "" Then Exit Sub
    MkDir x
    End Sub

  5. #5

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    I have to tell you, Matt's is a lot easier than Troy's, but they both to the same thing, create the folder. Is there any way that I can have them search for a particular directory or even folder to create a folder within that instead of typing the whole thing out? That is what both codes do as of now. Could I integrate this code into the box that comes up using Matt's code and then have that fill into the box, so that all the user has to do is name the new folder?

    Private Sub Command2_Click()
    Dim strResFolder As String
    strResFolder = BrowseForFolder(hwnd, "Please select a folder.")
    If strResFolder = "" Then
    Call MsgBox("The Cancel button was pressed.", vbExclamation)
    Else
    Call MsgBox("The folder " & strResFolder & " was selected.", vbExclamation)
    End If
    Text1 = strResFolder
    End Sub

    That allows me to browse in folders. Thank you for your help again!!

    ~Brian

  6. #6

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    I am sorry.. That was really wordy. All that I am looking for is a way to search for a particular folder, and then allow the user to create a new one within the folder or drive that has been searched. I am just trying to make it easier for the user so that they don't have to type the whole thing out. Thanks!

  7. #7
    Guest
    Nope, I understand what your trying to do and it is pretty easy.

    Assuming you have the BrowserForFolder code..even though I'm going to give it anyway. Here is what you want to do:

    Code:
    Option Explicit 
    
    Public Type BrowseInfo 
    hwndOwner As Long 
    pIDLRoot As Long 
    pszDisplayName As Long 
    lpszTitle As Long 
    ulFlags As Long 
    lpfnCallback As Long 
    lParam As Long 
    iImage As Long 
    End Type 
    
    Public Const BIF_RETURNONLYFSDIRS = 1 
    Public Const MAX_PATH = 260 
    
    Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) 
    Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long 
    Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long 
    Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long 
    
    
    Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
         
        'declare variables to be used
         Dim iNull As Integer
         Dim lpIDList As Long
         Dim lResult As Long
         Dim sPath As String
         Dim udtBI As BrowseInfo
    
        'initialise variables
         With udtBI
            .hwndOwner = hwndOwner
            .lpszTitle = lstrcat(sPrompt, "")
            .ulFlags = BIF_RETURNONLYFSDIRS
         End With
    
        'Call the browse for folder API
         lpIDList = SHBrowseForFolder(udtBI)
         
        'get the resulting string path
         If lpIDList Then
            sPath = String$(MAX_PATH, 0)
            lResult = SHGetPathFromIDList(lpIDList, sPath)
            Call CoTaskMemFree(lpIDList)
            iNull = InStr(sPath, vbNullChar)
            If iNull Then sPath = Left$(sPath, iNull - 1)
         End If
    
        'If cancel was pressed, sPath = ""
         BrowseForFolder = sPath
    
    End Function
    
    Private Sub Command1_Click() 
    Dim strResFolder As String 
    Dim x
    x = Inputbox("What is the name of the folder you would like to create?")
    If x = "" Then Exit Sub
    strResFolder = BrowseForFolder(hWnd, "What directory would you like the folder to be created in?") 
    If strResFolder <> "" Then 
    MkDir strResFolder & "\" & x
    Else 
    Msgbox "Cancel was pressed! Folder was not created!", 16
    Exit Sub
    End If 
    End Sub

  8. #8

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    Hat's off to you Matt. The coding works great.

    ~Brian

  9. #9
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305
    is there such a thing as "deldr" in order to delete a folder?

  10. #10

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    theman, I haven't heard of it, nor could I find anything about that particular command I did find one thing though...

    Private Sub Command10_Click()
    Dim fso As New FileSystemObject
    ' Create a new folder with the FileSystemObject object.
    fso.CreateFolder ("c:\Bogus")
    ' Get rid of the newly-created folder.
    fso.DeleteFolder ("c:\Bogus")
    Debug.Print "Deleted folder C:\Bogus"
    End Sub

    I hope that helps.



    Bit of a new problem though, in concern with the code that Matt sent me. Here it is under the command_click

    Private Sub Command3_Click()
    Dim strResFolder As String
    Dim x
    x = InputBox("What is the name of the folder you would like to create?")
    If x = "" Then
    MsgBox "Please Choose the other button to browse a folder", vbExclamation
    Exit Sub
    Else
    strResFolder = BrowseForFolder(hwnd, "What directory would you like the folder to be created in?")
    End If
    If strResFolder <> "" Then
    MkDir strResFolder & "\" & x
    MsgBox "You have choosen to place the file(s)/folders in the " & x & " folder under the directory " & strResFolder & "", vbExclamation
    Text1 = strResFolder & "\" & x
    Else
    MsgBox "Cancel was pressed! Folder was not created!", 16
    Exit Sub
    End If
    End Sub

    The problem is that the user creates a folder, but then can't directly put it on a drive that is selected (i.e. "H:\" ). The user has to select a folder within the drive (i.e. "H:\app\"). I believe it has to do with the part of the code that is:

    If strResFolder <> "" Then
    MkDir strResFolder & "\" & x

    b/c then you are mapping something like H:\\apps (this is my guess!!)

    I don't know what is the best way to fix this. If you oculd help me that would be great. I didn't put what I declared here to tap into the API's... I didn't think that that was the problem. I could be though. Thanks again for your help.

    ~Brian

  11. #11

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    There is no one out there that can help? What's up with that? I am sure there is a guru that knows how this stuff works...


    ~Brian

  12. #12
    Guest
    Originally posted by theman32x
    is there such a thing as "deldr" in order to delete a folder?
    theman32x, there is no such command. There is a command to remove a folder though. RmDir and here its very easy to use.

    Code:
    RmDir "C:\MyDir"
    Here is the solution to what you want to do fujiyama17.

    I included the Replace function only because I don't have VB6 yet and I needed to use it in order to rearrange the code around.

    Code:
    Public Function Replace(sIn As String, sFind As String, sReplace As String, Optional nStart As Long = 1, Optional nCount As Long = -1, Optional bCompare As VbCompareMethod = vbBinaryCompare) As String
        Dim nC As Long, nPos As Integer, sOut As String
        sOut = sIn
        nPos = InStr(nStart, sOut, sFind, bCompare)
        If nPos = 0 Then GoTo EndFn:
        Do
            nC = nC + 1
            sOut = Left(sOut, nPos - 1) & sReplace & Mid(sOut, nPos + Len(sFind))
            If nCount <> -1 And nC >= nCount Then Exit Do
            nPos = InStr(nStart, sOut, sFind, bCompare)
        Loop While nPos > 0
    EndFn:
        Replace = sOut
    End Function
    
    Dim strResFolder As String
    Dim x
    Dim folder, newfolder
    x = InputBox("What is the name of the folder you would like to create?")
    If x = "" Then
    MsgBox "Please Choose the other button to browse a folder", vbExclamation
    Exit Sub
    Else
    strResFolder = BrowseForFolder(hWnd, "What directory would you like the folder to be created in?")
    End If
    If strResFolder <> "" Then
    folder = Left$(strResFolder, 3)
    Debug.Print folder 
    '^will tell you which Drive has been selected.
    newfolder = Replace(strResFolder, "C:\", "H:\")
    '^replaces C:\ with H:\ like you wanted.
    MsgBox "You have choosen to place the file(s)/folders in the " & x & " folder under the directory " & newfolder & "", vbExclamation
    Text1 = newfolder & "\" & x
    Else
    MsgBox "Cancel was pressed! Folder was not created!", 16
    Exit Sub
    End If

  13. #13
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    thanks matthew

    i knew there was a command that short just didn't remember the syntax for it ...

  14. #14

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    Matthew,
    I really appreciate your help on this. It works great except for one prob. I am not wanting to replace C:\ with H:\ . THe problem is that I cannot select a root drvie such as C:\ or H:\ to put the created folder in. It has to be put into another folder within that drive to be saved. Can I make it so that I can select the drive and not have to select a folder within the drive to place the new folder? I have about 15 different drives that documents can be placed into. Thanks! Oh, uh, anyone's help is apprecated on this. Thank you!

    ~Brian

  15. #15
    Guest
    I don't think there is a Drive Dialog box, but you can do something like this:

    Code:
    Dim strDrive As String
    Dim x
    Dim folder, newfolder
    x = InputBox("What is the name of the folder you would like to create?")
    If x = "" Then
    MsgBox "Please Choose the other button to browse a folder", vbExclamation
    Exit Sub
    Else
    strDrive = BrowseForFolder(hWnd, "What Drive would you like the folder to be created in?")
    End If
    Drive = Len(strDrive)
    If Drive > 3 Then
    MsgBox "You have chosen a folder and not a Drive!  Please choose a Drive instead!", vbCritical
    Exit Sub
    End If
    If strDrive <> "" Then
    MsgBox "You have choosen to place the file(s)/folders in the " & x & " folder under the " & strDrive & " drive.", vbExclamation
    Else
    MsgBox "Cancel was pressed! Folder was not created!", vbCritical
    Exit Sub
    End If

  16. #16
    Guest
    Or an even better way might be to have the user type in the Drive he/she wants.

    Code:
    Dim StrDrive As String
    Dim x
    x = InputBox("What is the name of the folder you would like to create?")
    If x = "" Then
    MsgBox "Please Choose the other button to browse a folder", vbExclamation
    Exit Sub
    Else
    StrDrive = InputBox("What Drive would you like the folder to be created in?", , "C:\")
    End If
    If Len(StrDrive) > 3 Or Len(StrDrive) < 3 Then
    MsgBox "You have typed in a Drive that does not exist!", vbCritical
    Else
    If StrDrive <> "" Then
    'MkDir strDrive & "\" & x
    MsgBox "You have chosen to place the file(s)/folders in the " & x & " folder under the Drive:  " & StrDrive & "", vbExclamation
    Text1 = StrDrive & "" & x
    Else
    MsgBox "Cancel was pressed! Folder was not created!", 16
    Exit Sub
    End If
    End If

  17. #17

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    Sweetness,
    I'll try the coding out.

    ~Brian

Posting Permissions

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



Click Here to Expand Forum to Full Width