|
-
Mar 17th, 2005, 05:51 PM
#1
Thread Starter
Addicted Member
ARR! Directories....[RESOLVED]
Iv never used anything to do with directorys in Visual Basic really...but heres what i want to do the way im trying isnt working and once i get a code to open a browse window then im sure there much better ways to do this anyway i want to delete a file in a directory the file is called custom.hpk and the user will enter the directory in the text box e.g they enter C:\folder\ and then i want the program to add "custom.hpk" to the end of that i have this but it doesnt work
VB Code:
Kill ((Text1.Text) & "custom.hpk")
i assume its something simillar to that but maybe im missing out something
i need the code to browse for files aswell thx
Last edited by WilliamRobinson; Apr 18th, 2005 at 05:07 PM.
-
Mar 17th, 2005, 05:55 PM
#2
Re: ARR! Directories....
You have to do a check that the user entered the backslash at the end. Your code should wokr. Try changing the file's attributes to vbNormal before doing a kill, it might be set to read only.
For file browsing, you can use a combination of FileList, DirList and DriveList,
or use the Common Dialog control.
-
Mar 17th, 2005, 06:07 PM
#3
Thread Starter
Addicted Member
Re: ARR! Directories....
Backslash at the end? came up eeror and the file is not read only and id prefer not to use the dir and list boxes i want a "Browse" button and the window as if you were opening a file to come up
-
Mar 17th, 2005, 06:09 PM
#4
Thread Starter
Addicted Member
Re: ARR! Directories....
rofl! i got it now i relise what you mean with the backslash now omg how silly of me xD
do you have a code to browse for files now?
thx for fast reply
-
Mar 17th, 2005, 06:24 PM
#5
Re: ARR! Directories....
Add a common dialog control to your form, then put this code in the browse button click event:
VB Code:
dlgBrowse.Filter = "Text File (*.txt)|*.txt|Log File (*.log)|*.log||All Files (*.*)|*.*"
dlgBrowse.DialogTitle = "Open Log File"
dlgBrowse.ShowOpen
If dlgBrowse.FileName <> "" Then
txtLogFile.Text = dlgBrowse.FileName
End If
DoEvents
-
Mar 17th, 2005, 06:25 PM
#6
Re: ARR! Directories....
Forgot to mention:
-name the common dialog control "dlgBrowse"
-make a text box and name it "txtLogFile"
or edit the code and name it what you like, of course.
Also, not the dlgBrowse.Filter line and it's format. Change it to allow users
to open (see) only certain type of files.
-
Mar 17th, 2005, 06:44 PM
#7
Re: ARR! Directories....
playing counter-strike i see?
-
Mar 17th, 2005, 06:47 PM
#8
Thread Starter
Addicted Member
Re: ARR! Directories....
How do i add common dialog control?
-
Mar 17th, 2005, 06:51 PM
#9
Re: ARR! Directories....
right click the bar on the left of your screen (very long and slender) click components then microsoft common dialog control
-
Mar 17th, 2005, 07:00 PM
#10
Thread Starter
Addicted Member
Re: ARR! Directories....
Aww man that kicks ass lol what would be the code similar to that but to browse for directory
-
Mar 17th, 2005, 07:10 PM
#11
-
Mar 17th, 2005, 07:21 PM
#12
Thread Starter
Addicted Member
Re: ARR! Directories....
no, can you tell me anyway to select a directory and display the path in a textbox?
-
Mar 17th, 2005, 07:31 PM
#13
Re: ARR! Directories....
well, i do it like this
If Dir$(App.Path & "\saveto.txt") <> "" Then
msgbox "brtar"
-
Mar 17th, 2005, 07:33 PM
#14
Thread Starter
Addicted Member
Re: ARR! Directories....
i dont get how that will let the user browse for a specific folder, but ill play around with that code
-
Mar 17th, 2005, 07:50 PM
#15
Re: ARR! Directories....
Add the following code to a module.
VB Code:
Private Declare Function SHBrowseForFolder _
Lib "shell32" ( _
lpbi As BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList _
Lib "shell32" ( _
ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Private Type BROWSEINFO
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260
Public Function BrowseForFolder(OwnerForm As Form) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim tBrowseInfo As BROWSEINFO
With tBrowseInfo
.pszDisplayName = Space$(MAX_PATH)
.lpszTitle = "Select a folder"
.hWndOwner = OwnerForm.hWnd
.ulFlags = BIF_DONTGOBELOWDOMAIN + BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
BrowseForFolder = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
End If
End Function
You can then get a folder name by doing the following call from inside your form somewhere:
VB Code:
MsgBox BrowseForFolder(Me)
Last edited by Joacim Andersson; Mar 17th, 2005 at 07:54 PM.
-
Mar 17th, 2005, 08:12 PM
#16
Re: ARR! Directories....
no, can you tell me anyway to select a directory and display the path in a textbox?
what i said selects the dir, and you can see if it exists. If it does then you can do some stuff with it
-
Mar 17th, 2005, 08:20 PM
#17
Re: ARR! Directories....
 Originally Posted by |2eM!x
what i said selects the dir, and you can see if it exists. If it does then you can do some stuff with it
I think he wanted to show the browse for folder dialog box and let the user select a folder.
-
Mar 17th, 2005, 08:45 PM
#18
Re: ARR! Directories....
There are three ways I know of to browse for folders:
1. SHBrowseForFolder Function (Joacim's example) -
The downside to this is that as far as I can tell you can only set the root directory to one of the "special folders"
by setting the pidlRoot member of the BROWSEINFO structure
2. Create your own browse form by using a DriveList and DirList control
3. Use the Shell Object, code below
VB Code:
Option Explicit
'special folders
Private Const ssfALTSTARTUP = &H1D
Private Const ssfAPPDATA = &H1A
Private Const ssfBITBUCKET = &HA
Private Const ssfCOMMONALTSTARTUP = &H1E
Private Const ssfCOMMONAPPDATA = &H23
Private Const ssfCOMMONDESKTOPDIR = &H19
Private Const ssfCOMMONFAVORITES = &H1F
Private Const ssfCOMMONPROGRAMS = &H17
Private Const ssfCOMMONSTARTMENU = &H16
Private Const ssfCOMMONSTARTUP = &H18
Private Const ssfCONTROLS = &H3
Private Const ssfCOOKIES = &H21
Private Const ssfDESKTOP = &H0
Private Const ssfDESKTOPDIRECTORY = &H10
Private Const ssfDRIVES = &H11
Private Const ssfFAVORITES = &H6
Private Const ssfFONTS = &H14
Private Const ssfHISTORY = &H22
Private Const ssfINTERNETCACHE = &H20
Private Const ssfLOCALAPPDATA = &H1C
Private Const ssfMYPICTURES = &H27
Private Const ssfNETHOOD = &H13
Private Const ssfNETWORK = &H12
Private Const ssfPERSONAL = &H5
Private Const ssfPRINTERS = &H4
Private Const ssfPRINTHOOD = &H1B
Private Const ssfPROFILE = &H28
Private Const ssfPROGRAMFILES = &H26
Private Const ssfPROGRAMS = &H2
Private Const ssfRECENT = &H8
Private Const ssfSENDTO = &H9
Private Const ssfSTARTMENU = &HB
Private Const ssfSTARTUP = &H7
Private Const ssfSYSTEM = &H25
Private Const ssfTEMPLATES = &H15
Private Const ssfWINDOWS = &H24
'constants for Dialog box Options
Private Const BIF_VALIDATE = &H20 ' insist on valid result (or CANCEL)
Private Const BIF_BROWSEINCLUDEURLS = &H80 ' Allow URLs to be displayed or entered. (Requires BIF_USENEWUI)
Private Const BIF_UAHINT = &H100 ' Add a UA hint to the dialog, in place of the edit b= &h. May not be combined with BIF_EDITB= &h
Private Const BIF_BROWSEFORCOMPUTER = &H1000 ' Browsing for Computers.
Private Const BIF_BROWSEFORPRINTER = &H2000 ' Browsing for Printers
Private Const BIF_BROWSEINCLUDEFILES = &H4000 ' Browsing for Everything
Private Const BIF_DONTGOBELOWDOMAIN = &H2 ' For starting the Find Computer
Private Const BIF_EDITBOX = &H10 ' Add an editbox to the dialog
Private Const BIF_NEWDIALOGSTYLE = &H40 ' Use the new dialog layout with the ability to resize
Private Const BIF_NONEWFOLDERBUTTON = &H200 ' Do not add the "New Folder" button to the dialog. Only applicable with BIF_NEWDIALOGSTYLE.
Private Const BIF_NOTRANSLATETARGETS = &H400 ' don't traverse target as shortcut
' Caller needs to call OleInitialize() before using this API
Private Const BIF_RETURNONLYFSDIRS = &H1 ' For finding a folder to start document searching
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_SHAREABLE = &H8000 ' sharable resources displayed (remote shares, requires BIF_USENEWUI)
Private Const BIF_STATUSTEXT = &H4 ' Top of the dialog has 2 lines of text for BROWSEINFO.lpszTitle and one line if
Private Const BIF_USENEWUI = BIF_NEWDIALOGSTYLE Or BIF_EDITBOX
'Returns path to selected folder
' vRootFolder can be one of the special folders
' or a string to the root folder
Function fnShellBrowseForFolderVB(vRootFolder as Variant) As String
Dim fso
Dim objShell
Dim objFolder As Object
Dim iOptions As Long 'see constants in declarations
Set fso = CreateObject("Scripting.FileSystemObject")
iOptions = BIF_DONTGOBELOWDOMAIN Or BIF_RETURNONLYFSDIRS
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(Me.hWnd, _
"Browse for Folders", iOptions, vRootFolder)
If (Not objFolder Is Nothing) Then
'the following only works on local drives
fnShellBrowseForFolderVB = fso.GetAbsolutePathName(objFolder.Title)
End If
Set objFolder = Nothing
Set objShell = Nothing
End Function
The problem with this is that it only returns the directory name in the title property so you have to do some manipulation to get the fully qualified path. the method I used above only works for local drives. To include functionality for network drives you'll probably have to walk the folder object back to the root using the ParentFolder property (i.e. objFolder.ParentFolder.ParentFolder...etc
Last edited by moeur; Jan 27th, 2006 at 04:56 PM.
-
Mar 17th, 2005, 09:25 PM
#19
Re: ARR! Directories....
I think he wanted to show the browse for folder dialog box and let the user select a folder.
yeh, i just didnt understand it as that
-
Mar 17th, 2005, 11:01 PM
#20
Re: ARR! Directories....
 Originally Posted by moeur
There are three ways I know of to browse for folders:
1. SHBrowseForFolder Function (Joacim's example) -
The downside to this is that as far as I can tell you can only set the root directory to one of the "special folders" by setting the pidlRoot member of the BROWSEINFO structure
Yes but I don't see why that is a problem when you use the BIF_RETURNONLYFSDIRS flag. You can also specify any folder to be preselected when the dialog is shown if you like, but it requires a callback function.
The Shell object internally use the SHBrowseForFolder API.
-
Mar 18th, 2005, 01:50 AM
#21
Re: ARR! Directories....
Yes but I don't see why that is a problem when you use the BIF_RETURNONLYFSDIRS flag. You can also specify any folder to be preselected when the dialog is shown if you like, but it requires a callback function.
It is a problem if you don't want the user to be able to browse above a certain directory.
The Shell object internally use the SHBrowseForFolder API.
True, but as I mentioned, you can't set the root directory unless you can find the pidl for that directory. So far I have only found a way to do this for the "special" directories. I even posted a thread earlier asking for a solution to this and I got zero responses. I know you can do it, but I just don't know how.
-
Mar 18th, 2005, 09:05 AM
#22
Re: ARR! Directories....
 Originally Posted by moeur
It is a problem if you don't want the user to be able to browse above a certain directory.
True, but as I mentioned, you can't set the root directory unless you can find the pidl for that directory. So far I have only found a way to do this for the "special" directories. I even posted a thread earlier asking for a solution to this and I got zero responses. I know you can do it, but I just don't know how.
Aha... Now I see what you mean. I haven't seen your earlier thread about it but I hope you don't mind me posting it here instead. To get the PIDL for a path there is one correct way witch is pretty hard to do in VB and an easier way which is very easy. Well it's easy if you know about a little undocumented function in Shell32, that is. The function accepts a string, but as with all undocumented APIs there is not one Ansi version and one Wide version available so you need to pass a Unicode string if you're using a NT based system (incl. Win2000/XP) and an Ansi string if you pass it to a Win9x/Me system. Even though VB internally stores all strings in Unicode format it always converts them to an Ansi string before it passes it to an API function, so you have to convert the converted string back to Unicode. This sounds harder then it is, so I just show you the code instead:
VB Code:
Private Declare Function SHGetPIDLFromPath _
Lib "shell32" Alias "#162" ( _
ByVal sPath As String) As Long
Private Declare Function GetVersionEx _
Lib "kernel32" Alias "GetVersionExA" ( _
lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformID As Long
szCSDVersion As String * 128
End Type
Public Function GetPIDL(ByVal sPath As String) As Long
GetPIDL = SHGetPIDLFromPath(UnicodeCheck(sPath))
End Function
Private Function UnicodeCheck(str As String) As String
If IsWinNT Then
UnicodeCheck = StrConv(str, vbUnicode)
Else
UnicodeCheck = str
End If
End Function
Private Function IsWinNT() As Boolean
'Returns True if the current operating system is WinNT/2000/XP
Dim osvi As OSVERSIONINFO
osvi.dwOSVersionInfoSize = Len(osvi)
GetVersionEx osvi
IsWinNT = (osvi.dwPlatformID = VER_PLATFORM_WIN32_NT)
End Function
All you have to do is to call the GetPIDL function. So there is no reason to use the Microsoft Scripting Runtime.
-
Mar 18th, 2005, 09:33 AM
#23
Re: ARR! Directories....
I just tried your example on an XP platform and got the same results I got when I tried to solve this problem earlier. I made a change to your function
VB Code:
Public Function BrowseForFolder(OwnerForm As Form, [COLOR=Red]sPath[/COLOR] As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim tBrowseInfo As BROWSEINFO
With tBrowseInfo
.pszDisplayName = Space$(MAX_PATH)
.lpszTitle = "Select a folder"
.hWndOwner = OwnerForm.hWnd
.ulFlags = BIF_DONTGOBELOWDOMAIN + BIF_RETURNONLYFSDIRS
[COLOR=Red].pIDLRoot = GetPIDL(sPath)[/COLOR]
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
BrowseForFolder = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
End If
End Function
Then call the function
VB Code:
Debug.Print BrowseForFolder(Me, "C:\Windows")
I get a single folder that has a strange icon and will not expand.
Do you not get the same thing?
-
Mar 18th, 2005, 10:01 AM
#24
Re: ARR! Directories....
 Originally Posted by moeur
I get a single folder that has a strange icon and will not expand.
Do you not get the same thing?
Oh yeah... Strange! I've never used this code for anything but to show the the root folder of a drive as the root. Like C:\ or D:\ and that works just fine. I guess this needs to be investigated a bit more
-
Mar 18th, 2005, 04:49 PM
#25
Thread Starter
Addicted Member
Re: ARR! Directories....
Thanks For All The help guys i have it working now
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
|