Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mshta / select a file / filepath 1

Status
Not open for further replies.

fredmartinmaine

IS-IT--Management
Mar 23, 2015
79
US
I found this four-line vbscript fragment online someplace:

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>""")
sFileToProcess = oExec.StdOut.ReadLine
msgbox "The file you selected was " & chr(13) & sFileToProcess,0,"FILE TO PROCESS"

It allows the user to pick a file using a typical Windows dialog box. It appears to start in the folder where the script is executed. I'd expect that.

Does anyone know how to set the starting path? Say, the user's Documents folder or some static location maybe?

Fred

 
Code:
Function GetFileDlgEx(sIniDir,sFilter,sTitle) 
  Set oDlg = CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("&Len(sIniDir)+Len(sFilter)+Len(sTitle)+41&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />""") 
  oDlg.StdIn.Write "var iniDir='" & sIniDir & "';var filter='" & sFilter & "';var title='" & sTitle & "';" 
  GetFileDlgEx = oDlg.StdOut.ReadAll 
End Function

sample :

Code:
sIniDir = "C:\Windows\*.*pdf" ' extension must be preceded by an asterisk *.
sFilter = "Adobe pdf (*.pdf)|*.pdf|All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx|Image files (*.gif;*.png;*jpg;*.bmp)|*.gif;*.png;*jpg;*.bmp|Html files (*.htm;*.html;*.mht)|*.htm;*.html;*.mht|" 
sTitle = "GetFileDlg by omen999 2014 - [URL unfurl="true"]http://omen999.developpez.com"[/URL] 
rep = GetFileDlgEx(Replace(sIniDir,"\","\\"),sFilter,sTitle) 
MsgBox rep & vbcrlf & Len(rep)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top