提醒:本页面将不再更新、维护或者支持,文章、评论所叙述内容存在时效性,涉及技术细节或者软件使用方面不保证能够完全有效可操作,请谨慎参考!

最近在弄一些WScript脚本,有段功能需求就是弹出个打开文件夹的对话框,之前老是在" MSComDlg.CommonDialog "上纠结,后来才发现原来那个只能摆弄出文件选择对话框,后来在查阅了一些资料后才知道,要调用" Shell.Application "这个组件,哎,微软也应该把这些组件组合组合了,要不还真不容易找到。

文件夹浏览对话框

贴出部分代码,后来根据需要我还改写了JavaScript版本,话说JS的对象引用很是方便,不用像VBS那样Set对象了。

Const SHELL_MY_COMPUTER = &H11
Const SHELL_WINDOW_HANDLE = 0
Const SHELL_OPTIONS = 0
Function GetOpenDirectory(title)
	Dim ShlApp,ShlFdr,ShlFdrItem
	
	Set ShlApp = WSH.CreateObject("Shell.Application")
	Set ShlFdr = ShlApp.Namespace(SHELL_MY_COMPUTER)
	Set ShlFdrItem = ShlFdr.Self
	GetOpenDirectory = ShlFdrItem.Path
	Set ShlFdrItem = Nothing
	Set ShlFdr = Nothing
	
	Set ShlFdr = ShlApp.BrowseForFolder _
				(SHELL_WINDOW_HANDLE, _
				title, _
				SHELL_OPTIONS, _
				GetOpenDirectory)
	If ShlFdr Is Nothing Then
		GetOpenDirectory = ""
	Else
		Set ShlFdrItem = ShlFdr.Self
		GetOpenDirectory = ShlFdrItem.Path
		Set ShlFdrItem = Nothing
	End If
	Set ShlApp = Nothing
End Function
var SHELL_MY_COMPUTER = 0x11;
var SHELL_WINDOW_HANDLE = 0;
var SHELL_OPTIONS = 0;
	
function openDir(title) {
  var shlfdr,
  shlapp = WSH.CreateObject("Shell.Application");

  shlfdr = shlapp.BrowseForFolder(
			SHELL_WINDOW_HANDLE,
			title,
			SHELL_OPTIONS,
			shlapp.Namespace(SHELL_MY_COMPUTER).Self.Path);
  if (shlfdr == null)
     return "";
  return shlfdr.Self.Path;
}