Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
VBScript File Browser
'Bypasses IE7+ c:\fakepath\file.txt problem
Function BrowseForFile()
With CreateObject("WScript.Shell")
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName() & ".hta"
Dim path : path = "HKCU\Volatile Environment\MsgResp"
With tempFolder.CreateTextFile(tempName)
.Write "<input type=file name=f>" & _
"<script>f.click();(new ActiveXObject('WScript.Shell'))" & _
".RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value);" & _
"close();</script>"
.Close
End With
.Run tempFolder & "\" & tempName, 1, True
BrowseForFile = .RegRead(path)
.RegDelete path
fso.DeleteFile tempFolder & "\" & tempName
End With
End Function
MsgBox BrowseForFile
Function BrowseForFile()
Dim shell : Set shell = CreateObject("Shell.Application")
Dim file : Set file = shell.BrowseForFolder(0, "Choose a file:", &H4000, "C:\")
BrowseForFile = file.self.Path
End Function
MsgBox BrowseForFile
Option Explicit
' Flags for the options parameter
Const BIF_returnonlyfsdirs = &H0001
Const BIF_dontgobelowdomain = &H0002
Const BIF_statustext = &H0004
Const BIF_returnfsancestors = &H0008
Const BIF_editbox = &H0010
Const BIF_validate = &H0020
Const BIF_browseforcomputer = &H1000
Const BIF_browseforprinter = &H2000
Const BIF_browseincludefiles = &H4000
Dim file
file = BrowseForFolder( _
"Select a file or folder to copy", _
BIF_returnonlyfsdirs + BIF_browseincludefiles, _
"")
If file = "-5" Then
WScript.Echo "Not possible to select files in root folder"
Else
If file = "-1" Then
WScript.Echo "No object selected; Cancel clicked"
Else
WScript.Echo "Object: ", file
End If
End If
' Using the shell's BrowseForFolder method to
' return the full path to the selected object
' title = Text shown in the dialog box
' flag = One of the values for controlling the
' BrowseForFolder behavior
' dir = Preselected directory (can be "")
Function BrowseForFolder(title, flag, dir)
On Error Resume Next
Dim oShell, oItem, tmp
' Create WshShell object.
Set oShell = WScript.CreateObject("Shell.Application")
' Invoke Browse For Folder dialog box.
Set oItem = oShell.BrowseForFolder(&H0, title, flag, dir)
If Err.Number <> 0 Then
If Err.Number = 5 Then
BrowseForFolder= "-5"
Err.Clear
Set oShell = Nothing
Set oItem = Nothing
Exit Function
End If
End If
' Now we try to retrieve the full path.
BrowseForFolder = oItem.ParentFolder.ParseName(oItem.Title).Path
' Handling: Cancel button and selecting a drive
If Err<> 0 Then
If Err.Number = 424 Then ' Handle Cancel button.
BrowseForFolder = "-1"
Else
Err.Clear
' Handle situation in which user selects a drive.
' Extract drive letter from the title--first search
' for a colon (:).
tmp = InStr(1, oItem.Title, ":")
If tmp > 0 Then ' A : is found; use two
' characters and add \.
BrowseForFolder = _
Mid(oItem.Title, (tmp - 1), 2) & "\"
End If
End If
End If
Set oShell = Nothing
Set oItem = Nothing
On Error GoTo 0
End Function
@akatukiyo

This comment has been minimized.

Copy link

@akatukiyo akatukiyo commented Feb 20, 2015

browser2.vbs The 2nd vbs does not work, Error when run for picking up files. How can you make it start up from a fixed location? I like the 2nd dialog window only with a OK button to select file or folder. Can you fix the file selection in 2nd vbs? Thanks

@mlhaufe

This comment has been minimized.

Copy link
Owner Author

@mlhaufe mlhaufe commented Jul 12, 2015

browser2.vbs has been updated to specify an explicit starting path. This should solve the problem of mystery errors happening for some people.

@zwfl

This comment has been minimized.

Copy link

@zwfl zwfl commented May 19, 2016

The Dialog works fine but I only get "No object selected; Cancel clicked" (BrowseForFolder = -1) in Shellbrowse.vbs when I try to choose a file. If I choose a folder i get the path as return value.

@manuganu9

This comment has been minimized.

Copy link

@manuganu9 manuganu9 commented Jul 29, 2016

The dialog works fine in browse.vbs. But it's showing all files of folder.

I want to add filter for (*.jpg) files..Is it possible?

@mlhaufe

This comment has been minimized.

Copy link
Owner Author

@mlhaufe mlhaufe commented Aug 4, 2019

@manuganu9

browse.vbs relies on generating a *.hta file to trigger the browse dialog. HTA files use Internet Explorer 5 - 9 (depending on what is installed on your machine). If a newer version of Internet Explorer is installed, the HTA will still use IE9.

With that said, the feature you want is the 'accept' attribute of the element:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept

The problem though is that IE9 does not support this attribute:

https://www.caniuse.com/#search=accept

So no, it's not possible through the approach used in browse.vbs

@mlhaufe

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment