« VBS - Duplicate Folder Structure | Main | Dim Dim D'oh »

VBScript - Select a Folder

Have you ever wanted to use the browse for a folder dialog box from your VB Script?  Well maybe you can make use of this function?  It's an amalgamation of various other scripts tailored to my own needs so hopefully it'll suit your purposes too, but if not just tweak it until it does.

Option Explicit

Function SelectFolder(pstrDialogLabel)
	'Select a folder
	Const BIF_returnonlyfsdirs   = &H0001
	Const BIF_editbox            = &H0010

	Dim objBrowseFolderDialog, objFolder, objFSO, objSelection
	Dim bBrowseForFolder

	Set objBrowseFolderDialog = WScript.CreateObject("Shell.Application")

	bBrowseForFolder = true

	While bBrowseForFolder
		Set objFolder = objBrowseFolderDialog.BrowseForFolder (&H0, pstrDialogLabel, BIF_editbox + BIF_returnonlyfsdirs)

		'Check that something has been returned
		If IsValidFolder(objFolder) Then
			Set objFSO = CreateObject("Scripting.FileSystemObject")

			Set objSelection = objFolder.Self
			If objFSO.FolderExists(objSelection.Path) Then
				'A valid folder has been selected
				SelectFolder = objSelection.Path
				bBrowseForFolder = false
				'The selection is not a valid folder, try again...
				MsgBox objFolder.Title & " is not a valid folder, please select another folder" _
					, vbOKOnly & vbExclamation, "Invalid Selection"
			End If
			'Nothing was selected, so return a null string
			SelectFolder = ""
			bBrowseForFolder = false
		End If
End Function

Function IsValidFolder(pobjFolder)
	'Check that we have a valid value
	'i.e. you can concatenate it to a string
	Dim strTest

	On Error Resume Next

	strTest = " " & pobjFolder

	If Err <> 0 Then
		IsValidFolder = false
		IsValidFolder = true
	End If

	On Error GoTo 0
End Function

Browse for folder Browse for folderTo use it simply pass the function a bit of text that you would like to display at the top of the browse dialog box and the function will then return the path to the folder.  The function will force the user to select a folder item with a "proper" path so items such as 'My Computer' and 'My Network Places' will prompt the user to reselect a folder item.  Should the user cancel then an empty string will be returned by the function.

An example call might look like:

MsgBox SelectFolder("Please select a folder")

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (7)

[...] IT The micro-blog for Reboot IT « VBScript - Select a Folder VBS - Duplicate Folder Structure 28/03/2009 Armed with my folder browser script I finally [...]

[...] to produce more than 100 PDFs meant that a little help could come in handy.   Making use of the select folder VBScript I refined a little while ago, I created a generic VBScript to take a folder selection from a user [...]

This code doesn't seem to work.

August 6, 2009 | Unregistered CommenterSquish

I've tested and used this code many times and whilst I'm sure I could find ways to improve it, it does work for me.

The main code is just a couple of functions. Have you tried calling the first function? There is an example line of code utilising a MsgBox call of this function at the end of the post.

If you can provide a bit more detail about what is actually happening and what you see (or don't), I might be able to help further.

August 7, 2009 | Unregistered Commenterthermalspindle

[...] 3. VBScript – Select a Folder [...]

Dude, the code works great. it is just a matter of correctly calling the right function.

The programming is flawless!!!


February 17, 2010 | Unregistered CommenterMichael

A too complicated function, also that IsValidFolder is abs useless.
Here is what I use:
Function SelectFolder(sLabel)
Dim objShell, objFolder
SelectFolder = vbNullString
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, sLabel, 0)
If objFolder Is Nothing Then Exit Function
SelectFolder = objFolder.Self.Path
If AscW(SelectFolder) = 58 Then SelectFolder = vbNullString
End Function


December 5, 2012 | Unregistered CommenterPanayot

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>