10

At present I am opening a file with my vbscript as follows:

strFile = "C:\Users\test\file.txt"
Set objFile = objFSO.OpenTextFile(strFile)

I would like to change this so that a file can be selected/navigated to by the user and that file is used in the script. How can I add this ability? I have tried to search for how to load a file dialog/prompt the user for a file etc just not sure how to complete in a VBScript.

Steven Doggart
  • 42,597
  • 8
  • 69
  • 103
StuartM
  • 6,563
  • 16
  • 79
  • 153

3 Answers3

31

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.ReadLine
wscript.echo sFileSelected
PatricK
  • 6,305
  • 1
  • 19
  • 25
  • 1
    Thanks. Tested successfully on 64-bit Windows 10 Pro. – kol Mar 04 '18 at 14:43
  • I like this solution, is it possible to set a filter to a filetype (i.e. *.txt)? – Erik Apr 05 '19 at 04:32
  • @Erik This [Old SO post](https://stackoverflow.com/questions/38369664/how-to-add-filter-to-a-file-chooser-in-batch) may help you. – PatricK Apr 08 '19 at 01:53
  • @Erik Somehow, I don't like it. It seems kinda hacky that you need to do it this way. All other VBS solutions I've come across use a weird version that isn't provided by the windows explorer to pick a file. But since it is just HTML, you would filter out any file like you would in a HTML document. – J-Cake Jan 11 '20 at 22:33
  • Nice. Here is a commented function based on the same code: https://www.robvanderwoude.com/vbstech_ui_fileopen.php#ExecMSHTA – Andre Feb 14 '22 at 12:09
2

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 If
End Function
Nathan Rice
  • 3,071
  • 1
  • 19
  • 30
0

VBSEdit program installs a COM library VBSEdit Toolkit, which allows Open File dialogs.

From VBSEdit help:

Dialog boxes 
OpenFileDialog method
Prompt the user to open a file
toolkit.OpenFileDialog ([initialFolder,[filters,[multiselect,[title]]]]) 

'Opens a single file
Set 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.Quit
End If

'Opens multiple files
Set 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
  Next
Else
  Wscript.Quit
End If



SaveFileDialog method
Prompt the user to save a file
toolkit.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.Close
Else
  Wscript.Quit
End If



SelectFolder method
Prompt the user to select a folder
toolkit.SelectFolder ([initialDir,[title]]) 

Set toolkit = CreateObject("VbsEdit.Toolkit")
myfolder=toolkit.SelectFolder("c:\scripts\","Please select a folder")

If Not(IsNull(myfolder)) Then
  WScript.Echo myfolder
Else
  Wscript.Quit
End If

(Actually, folder selection dialogs can be opened without VBSEdit toolkit, with BrowseForFolder method of Shell.Application object.)

Hermolaou
  • 23
  • 5