Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for brfolder.vbs

(view source code of brfolder.vbs as plain text)

  1. Option Explicit
  2.  
  3. WScript.Echo BrowseFolder( "C:\Program Files", True )
  4. WScript.Echo BrowseFolder( "My Computer", False )
  5. WScript.Echo BrowseFolder( "", False )
  6.  
  7.  
  8. Function BrowseFolder( myStartLocation, blnSimpleDialog )
  9. ' This function generates a Browse Folder dialog
  10. ' and returns the selected folder as a string.
  11. '
  12. ' Arguments:
  13. ' myStartLocation   [string]  start folder for dialog, or "My Computer", or
  14. '                             empty string to open in "Desktop\My Documents"
  15. ' blnSimpleDialog   [boolean] if False, an additional text field will be
  16. '                             displayed where the folder can be selected
  17. '                             by typing the fully qualified path
  18. '
  19. ' Returns:          [string]  the fully qualified path to the selected folder
  20. '
  21. ' Based on the Hey Scripting Guys article
  22. ' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
  23. ' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
  24. '
  25. ' Function written by Rob van der Woude
  26. ' http://www.robvanderwoude.com
  27. 	Const MY_COMPUTER   = &H11&
  28. 	Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
  29.  
  30. 	Dim numOptions, objFolder, objFolderItem
  31. 	Dim objPath, objShell, strPath, strPrompt
  32.  
  33. 	' Set the options for the dialog window
  34. 	strPrompt = "Select a folder:"
  35. 	If blnSimpleDialog = True Then
  36. 		numOptions = 0      ' Simple dialog
  37. 	Else
  38. 		numOptions = &H10&  ' Additional text field to type folder path
  39. 	End If
  40.  
  41. 	' Create a Windows Shell object
  42. 	Set objShell = CreateObject( "Shell.Application" )
  43.  
  44. 	' If specified, convert "My Computer" to a valid
  45. 	' path for the Windows Shell's BrowseFolder method
  46. 	If UCase( myStartLocation ) = "MY COMPUTER" Then
  47. 		Set objFolder = objShell.Namespace( MY_COMPUTER )
  48. 		Set objFolderItem = objFolder.Self
  49. 		strPath = objFolderItem.Path
  50. 	Else
  51. 		strPath = myStartLocation
  52. 	End If
  53.  
  54. 	Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
  55. 	                                          numOptions, strPath )
  56.  
  57. 	' Quit if no folder was selected
  58. 	If objFolder Is Nothing Then
  59.     	BrowseFolder = ""
  60.     	Exit Function
  61. 	End If
  62.  
  63. 	' Retrieve the path of the selected folder
  64. 	Set objFolderItem = objFolder.Self
  65. 	objPath = objFolderItem.Path
  66.  
  67. 	' Return the path of the selected folder
  68. 	BrowseFolder = objPath
  69. End Function
  70.  

page last modified: 2024-04-16; loaded in 0.0148 seconds