|
-
Aug 23rd, 2000, 02:58 PM
#1
Thread Starter
Lively Member
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.
-
Aug 23rd, 2000, 03:09 PM
#2
Hyperactive Member
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!!!
-
Aug 23rd, 2000, 03:11 PM
#3
Hyperactive Member
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
-
Aug 23rd, 2000, 03:22 PM
#4
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
-
Aug 23rd, 2000, 03:33 PM
#5
Thread Starter
Lively Member
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
-
Aug 23rd, 2000, 03:36 PM
#6
Thread Starter
Lively Member
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!
-
Aug 23rd, 2000, 04:03 PM
#7
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
-
Aug 24th, 2000, 08:06 AM
#8
Thread Starter
Lively Member
Hat's off to you Matt. The coding works great.
~Brian
-
Aug 24th, 2000, 11:14 AM
#9
Hyperactive Member
is there such a thing as "deldr" in order to delete a folder?
-
Aug 24th, 2000, 12:11 PM
#10
Thread Starter
Lively Member
-
Aug 24th, 2000, 01:12 PM
#11
Thread Starter
Lively Member
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
-
Aug 24th, 2000, 05:03 PM
#12
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.
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
-
Aug 24th, 2000, 06:00 PM
#13
Hyperactive Member
thanks matthew
i knew there was a command that short just didn't remember the syntax for it ...
-
Aug 25th, 2000, 08:13 AM
#14
Thread Starter
Lively Member
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
-
Aug 25th, 2000, 09:35 PM
#15
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
-
Aug 25th, 2000, 09:51 PM
#16
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
-
Aug 28th, 2000, 07:50 AM
#17
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|