Results 1 to 25 of 25

Thread: ARR! Directories....[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Resolved 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:
    1. 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.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  3. #3

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    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

  4. #4

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Talking 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

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: ARR! Directories....

    Add a common dialog control to your form, then put this code in the browse button click event:

    VB Code:
    1. dlgBrowse.Filter = "Text File (*.txt)|*.txt|Log File (*.log)|*.log||All Files (*.*)|*.*"
    2.         dlgBrowse.DialogTitle = "Open Log File"
    3.         dlgBrowse.ShowOpen
    4.         If dlgBrowse.FileName <> "" Then
    5.             txtLogFile.Text = dlgBrowse.FileName
    6.         End If
    7.         DoEvents

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: ARR! Directories....

    playing counter-strike i see?

  8. #8

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Red face Re: ARR! Directories....

    How do i add common dialog control?

  9. #9
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: ARR! Directories....

    right click the bar on the left of your screen (very long and slender) click components then microsoft common dialog control

  10. #10

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Re: ARR! Directories....

    Aww man that kicks ass lol what would be the code similar to that but to browse for directory

  11. #11
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: ARR! Directories....


  12. #12

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Re: ARR! Directories....

    no, can you tell me anyway to select a directory and display the path in a textbox?

  13. #13
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: ARR! Directories....

    well, i do it like this
    If Dir$(App.Path & "\saveto.txt") <> "" Then
    msgbox "brtar"

  14. #14

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Re: ARR! Directories....

    i dont get how that will let the user browse for a specific folder, but ill play around with that code

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ARR! Directories....

    Add the following code to a module.
    VB Code:
    1. Private Declare Function SHBrowseForFolder _
    2.  Lib "shell32" ( _
    3.  lpbi As BROWSEINFO) As Long
    4.  
    5. Private Declare Function SHGetPathFromIDList _
    6.  Lib "shell32" ( _
    7.  ByVal pidList As Long, _
    8.  ByVal lpBuffer As String) As Long
    9.  
    10. Private Type BROWSEINFO
    11.    hWndOwner As Long
    12.    pIDLRoot As Long
    13.    pszDisplayName As String
    14.    lpszTitle As String
    15.    ulFlags As Long
    16.    lpfnCallback As Long
    17.    lParam As Long
    18.    iImage As Long
    19. End Type
    20.  
    21. Private Const BIF_RETURNONLYFSDIRS = 1
    22. Private Const BIF_DONTGOBELOWDOMAIN = 2
    23. Private Const MAX_PATH = 260
    24.  
    25. Public Function BrowseForFolder(OwnerForm As Form) As String
    26.     Dim lpIDList As Long
    27.     Dim sBuffer As String
    28.     Dim tBrowseInfo As BROWSEINFO
    29.    
    30.     With tBrowseInfo
    31.         .pszDisplayName = Space$(MAX_PATH)
    32.         .lpszTitle = "Select a folder"
    33.         .hWndOwner = OwnerForm.hWnd
    34.         .ulFlags = BIF_DONTGOBELOWDOMAIN + BIF_RETURNONLYFSDIRS
    35.     End With
    36.    
    37.     lpIDList = SHBrowseForFolder(tBrowseInfo)
    38.     If (lpIDList) Then
    39.         sBuffer = Space(MAX_PATH)
    40.         SHGetPathFromIDList lpIDList, sBuffer
    41.         BrowseForFolder = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
    42.     End If
    43. End Function
    You can then get a folder name by doing the following call from inside your form somewhere:
    VB Code:
    1. MsgBox BrowseForFolder(Me)
    Last edited by Joacim Andersson; Mar 17th, 2005 at 07:54 PM.

  16. #16
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ARR! Directories....

    Quote 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.

  18. #18
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Option Explicit
    2.  
    3. 'special folders
    4.     Private Const ssfALTSTARTUP = &H1D
    5.     Private Const ssfAPPDATA = &H1A
    6.     Private Const ssfBITBUCKET = &HA
    7.     Private Const ssfCOMMONALTSTARTUP = &H1E
    8.     Private Const ssfCOMMONAPPDATA = &H23
    9.     Private Const ssfCOMMONDESKTOPDIR = &H19
    10.     Private Const ssfCOMMONFAVORITES = &H1F
    11.     Private Const ssfCOMMONPROGRAMS = &H17
    12.     Private Const ssfCOMMONSTARTMENU = &H16
    13.     Private Const ssfCOMMONSTARTUP = &H18
    14.     Private Const ssfCONTROLS = &H3
    15.     Private Const ssfCOOKIES = &H21
    16.     Private Const ssfDESKTOP = &H0
    17.     Private Const ssfDESKTOPDIRECTORY = &H10
    18.     Private Const ssfDRIVES = &H11
    19.     Private Const ssfFAVORITES = &H6
    20.     Private Const ssfFONTS = &H14
    21.     Private Const ssfHISTORY = &H22
    22.     Private Const ssfINTERNETCACHE = &H20
    23.     Private Const ssfLOCALAPPDATA = &H1C
    24.     Private Const ssfMYPICTURES = &H27
    25.     Private Const ssfNETHOOD = &H13
    26.     Private Const ssfNETWORK = &H12
    27.     Private Const ssfPERSONAL = &H5
    28.     Private Const ssfPRINTERS = &H4
    29.     Private Const ssfPRINTHOOD = &H1B
    30.     Private Const ssfPROFILE = &H28
    31.     Private Const ssfPROGRAMFILES = &H26
    32.     Private Const ssfPROGRAMS = &H2
    33.     Private Const ssfRECENT = &H8
    34.     Private Const ssfSENDTO = &H9
    35.     Private Const ssfSTARTMENU = &HB
    36.     Private Const ssfSTARTUP = &H7
    37.     Private Const ssfSYSTEM = &H25
    38.     Private Const ssfTEMPLATES = &H15
    39.     Private Const ssfWINDOWS = &H24
    40.  
    41. 'constants for Dialog box Options
    42. Private Const BIF_VALIDATE = &H20               ' insist on valid result (or CANCEL)
    43. Private Const BIF_BROWSEINCLUDEURLS = &H80      ' Allow URLs to be displayed or entered. (Requires BIF_USENEWUI)
    44. 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
    45.  
    46. Private Const BIF_BROWSEFORCOMPUTER = &H1000   ' Browsing for Computers.
    47. Private Const BIF_BROWSEFORPRINTER = &H2000    ' Browsing for Printers
    48. Private Const BIF_BROWSEINCLUDEFILES = &H4000  ' Browsing for Everything
    49. Private Const BIF_DONTGOBELOWDOMAIN = &H2      ' For starting the Find Computer
    50. Private Const BIF_EDITBOX = &H10               ' Add an editbox to the dialog
    51. Private Const BIF_NEWDIALOGSTYLE = &H40         ' Use the new dialog layout with the ability to resize
    52. Private Const BIF_NONEWFOLDERBUTTON = &H200     ' Do not add the "New Folder" button to the dialog.  Only applicable with BIF_NEWDIALOGSTYLE.
    53. Private Const BIF_NOTRANSLATETARGETS = &H400    ' don't traverse target as shortcut
    54.                                         ' Caller needs to call OleInitialize() before using this API
    55. Private Const BIF_RETURNONLYFSDIRS = &H1       ' For finding a folder to start document searching
    56. Private Const BIF_RETURNFSANCESTORS = &H8
    57. Private Const BIF_SHAREABLE = &H8000           ' sharable resources displayed (remote shares, requires BIF_USENEWUI)
    58. Private Const BIF_STATUSTEXT = &H4              ' Top of the dialog has 2 lines of text for BROWSEINFO.lpszTitle and one line if
    59. Private Const BIF_USENEWUI = BIF_NEWDIALOGSTYLE Or BIF_EDITBOX
    60.  
    61.  
    62. 'Returns path to selected folder
    63. ' vRootFolder can be one of the special folders
    64. '  or a string to the root folder
    65.  Function fnShellBrowseForFolderVB(vRootFolder as Variant) As String
    66.         Dim fso
    67.         Dim objShell
    68.         Dim objFolder As Object
    69.         Dim iOptions As Long 'see constants in declarations
    70.        
    71.         Set fso = CreateObject("Scripting.FileSystemObject")
    72.         iOptions = BIF_DONTGOBELOWDOMAIN Or BIF_RETURNONLYFSDIRS
    73.        
    74.         Set objShell = CreateObject("Shell.Application")
    75.             Set objFolder = objShell.BrowseForFolder(Me.hWnd, _
    76.               "Browse for Folders", iOptions, vRootFolder)
    77.                 If (Not objFolder Is Nothing) Then
    78.                     'the following only works on local drives
    79.                     fnShellBrowseForFolderVB = fso.GetAbsolutePathName(objFolder.Title)
    80.                 End If
    81.             Set objFolder = Nothing
    82.         Set objShell = Nothing
    83. 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.

  19. #19
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  20. #20
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ARR! Directories....

    Quote 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.

  21. #21
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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.

  22. #22
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ARR! Directories....

    Quote 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:
    1. Private Declare Function SHGetPIDLFromPath _
    2.  Lib "shell32" Alias "#162" ( _
    3.  ByVal sPath As String) As Long
    4.  
    5. Private Declare Function GetVersionEx _
    6.  Lib "kernel32" Alias "GetVersionExA" ( _
    7.  lpVersionInformation As OSVERSIONINFO) As Long
    8.  
    9. Private Type OSVERSIONINFO
    10.     dwOSVersionInfoSize As Long
    11.     dwMajorVersion As Long
    12.     dwMinorVersion As Long
    13.     dwBuildNumber As Long
    14.     dwPlatformID As Long
    15.     szCSDVersion As String * 128
    16. End Type
    17.  
    18. Public Function GetPIDL(ByVal sPath As String) As Long
    19.     GetPIDL = SHGetPIDLFromPath(UnicodeCheck(sPath))
    20. End Function
    21.  
    22. Private Function UnicodeCheck(str As String) As String
    23.     If IsWinNT Then
    24.         UnicodeCheck = StrConv(str, vbUnicode)
    25.     Else
    26.         UnicodeCheck = str
    27.     End If
    28. End Function
    29.  
    30. Private Function IsWinNT() As Boolean
    31.   'Returns True if the current operating system is WinNT/2000/XP
    32.    Dim osvi As OSVERSIONINFO
    33.    osvi.dwOSVersionInfoSize = Len(osvi)
    34.    GetVersionEx osvi
    35.    IsWinNT = (osvi.dwPlatformID = VER_PLATFORM_WIN32_NT)
    36. End Function
    All you have to do is to call the GetPIDL function. So there is no reason to use the Microsoft Scripting Runtime.

  23. #23
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Public Function BrowseForFolder(OwnerForm As Form, [COLOR=Red]sPath[/COLOR] As String) As String
    2.     Dim lpIDList As Long
    3.     Dim sBuffer As String
    4.     Dim tBrowseInfo As BROWSEINFO
    5.    
    6.     With tBrowseInfo
    7.         .pszDisplayName = Space$(MAX_PATH)
    8.         .lpszTitle = "Select a folder"
    9.         .hWndOwner = OwnerForm.hWnd
    10.         .ulFlags = BIF_DONTGOBELOWDOMAIN + BIF_RETURNONLYFSDIRS
    11.         [COLOR=Red].pIDLRoot = GetPIDL(sPath)[/COLOR]
    12.     End With
    13.         lpIDList = SHBrowseForFolder(tBrowseInfo)
    14.     If (lpIDList) Then
    15.         sBuffer = Space(MAX_PATH)
    16.         SHGetPathFromIDList lpIDList, sBuffer
    17.         BrowseForFolder = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
    18.     End If
    19. End Function
    Then call the function
    VB Code:
    1. 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?

  24. #24
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ARR! Directories....

    Quote 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

  25. #25

    Thread Starter
    Addicted Member WilliamRobinson's Avatar
    Join Date
    Feb 2005
    Posts
    219

    Talking 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
  •  



Click Here to Expand Forum to Full Width