VBScript to open a dialog to select a filepath
There is another solution I found interesting from MS TechNet less customization but gets what you wanted to achieve. This returns the full path of the selected file.
Set wShell=CreateObject("WScript.Shell")Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")sFileSelected = oExec.StdOut.ReadLinewscript.echo sFileSelected
Here you go:
http://www.robvanderwoude.com/vbstech_ui_fileopen.php
strFile = GetFileName("C:\Users\test\", "Text files|*.txt")Set objFile = objFSO.OpenTextFile(strFile)Function GetFileName( myDir, myFilter ) ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Standard housekeeping Dim objDialog ' Create a dialog object Set objDialog = CreateObject( "UserAccounts.CommonDialog" ) ' Check arguments and use defaults when necessary If myDir = "" Then ' Default initial folder is "My Documents" objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" ) Else ' Use the specified initial folder objDialog.InitialDir = myDir End If If myFilter = "" Then ' Default file filter is "All files" objDialog.Filter = "All files|*.*" Else ' Use the specified file filter objDialog.Filter = myFilter End If ' Open the dialog and return the selected file name If objDialog.ShowOpen Then GetFileName = objDialog.FileName Else GetFileName = "" End IfEnd Function
VBSEdit program installs a COM library VBSEdit Toolkit, which allows Open File dialogs.
From VBSEdit help:
Dialog boxes OpenFileDialog methodPrompt the user to open a filetoolkit.OpenFileDialog ([initialFolder,[filters,[multiselect,[title]]]]) 'Opens a single fileSet toolkit = CreateObject("VbsEdit.Toolkit")files=toolkit.OpenFileDialog("c:\scripts\","Text Files (*.txt)|*.txt",False,"Open a text file")If UBound(files)>=0 Then WScript.Echo files(0)Else Wscript.QuitEnd If'Opens multiple filesSet toolkit = CreateObject("VbsEdit.Toolkit")files=toolkit.OpenFileDialog("c:\scripts\","Text Files (*.txt)|*.txt",True,"Open a text file")If UBound(files)>=0 Then For Each filepath In files WScript.Echo filepath NextElse Wscript.QuitEnd IfSaveFileDialog methodPrompt the user to save a filetoolkit.SaveFileDialog ([initialFolder,[initialFilename,[filters,[title]]]]) Set toolkit = CreateObject("VbsEdit.Toolkit")filepath = toolkit.SaveFileDialog("c:\scripts","test.txt","Text Files (*.txt)|*.txt")If Not(IsNull(filepath)) Then Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(filepath,True) objFile.WriteLine Date objFile.CloseElse Wscript.QuitEnd IfSelectFolder methodPrompt the user to select a foldertoolkit.SelectFolder ([initialDir,[title]]) Set toolkit = CreateObject("VbsEdit.Toolkit")myfolder=toolkit.SelectFolder("c:\scripts\","Please select a folder")If Not(IsNull(myfolder)) Then WScript.Echo myfolderElse Wscript.QuitEnd If
…
(Actually, folder selection dialogs can be opened without VBSEdit toolkit, with BrowseForFolder method of Shell.Application object.)