Board logo

标题: [转贴] VBS 文件选择对话框(无临时文件) [打印本页]

作者: yu2n    时间: 2015-11-5 01:02     标题: VBS 文件选择对话框(无临时文件)

本帖最后由 yu2n 于 2015-11-5 01:05 编辑

VBS 文件选择对话框(无临时文件)
Win7x64 测试通过
  1. '0.vbs
  2. 'How can I filter file type extensions using CreateObject("Scripting.FileSystemObject")?
  3. 'http://www.visualbasicscript.com/How-can-I-filter-file-type-extensions-using-CreateObjectquotScriptingFileSystemObjectquot-m105786.aspx
  4. '初始目录、文件过滤、标题
  5. StrIniDir = ""
  6. StrFilter = "Msi Files (*.msi)|*.msi|All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx|Adobe pdf (*.pdf)|*.pdf|"
  7. StrTitle = "Select MSI file"
  8. Msgbox GetFileDlg(Replace(StrIniDir,"\","\\"),StrFilter,StrTitle)
  9. Msgbox GetFileDlgBar(Replace(StrIniDir,"\","\\"),StrFilter,StrTitle)
  10. '不在任务栏显示选择框
  11. Function GetFileDlg(sIniDir,sFilter,sTitle)
  12. On Error Resume Next
  13. GetFileDlg=CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg('" & sIniDir & "',null,'" & sFilter & "','" & sTitle & "')));close();}</script><hta:application showintaskbar=no />""").StdOut.ReadAll
  14. End Function
  15. '在任务栏显示选择框
  16. Function GetFileDlgBar(sIniDir,sFilter,sTitle)
  17. On Error Resume Next
  18. GetFileDlgBar=CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg('" & sIniDir & "',null,'" & sFilter & "','" & sTitle & "')));close();}</script>""").StdOut.ReadAll
  19. End Function
复制代码

作者: Demon    时间: 2015-11-5 12:16

收藏
作者: CrLf    时间: 2015-11-5 16:19

马克
作者: wankoilz    时间: 2015-11-25 16:16

也收藏一个,学习学习!
作者: yu2n    时间: 2015-11-26 08:42

同理,以下代码也实现了文件选择框功能(无临时文件),只是没有“文件类型过滤”+“自定义标题”功能:
  1. '获取单个文件选择 By Yu2n
  2. Function BrowseForFileByHtaInput()
  3. Dim wShell, oExec
  4. Set wShell=CreateObject("WScript.Shell")
  5. Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=f>" & _
  6. "<script>moveTo(0,-9999);resizeTo(0,0);f.click();" & _
  7. "new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1)." & _
  8. "WriteLine(f.value);close();</script>""")
  9. BrowseForFileByHtaInput = oExec.StdOut.ReadLine
  10. End Function
  11. Msgbox "Select File: " & vbCrLf & BrowseForFileByHtaInput(), vbSystemModal+vbInformation, WScript.ScriptName
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2