【发布时间】:2015-10-17 19:39:23
【问题描述】:
我目前正在开发一个可以启动其他程序的程序。它是批量的。但我需要它对用户友好。我想要一个可以打开文件选择窗口的代码,并将我选择的目录保存到 txt 文件(或 csv 或其他文件)中。我是 VBS 的新手,如果我遗漏了一些简单的东西,请原谅我。但是我搜索了一段时间并接近了,结果失败了。
这是我目前所拥有的......
Set shell = CreateObject( "WScript.Shell" )
defaultLocalDir = shell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop"
Set shell = Nothing
file = ChooseFile(defaultLocalDir)
wscript.echo file
Function ChooseFile (ByVal initialDir)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
Dim winVersion
' This collection should contain just the one item
For Each objItem in colItems
'Caption e.g. Microsoft Windows 7 Professional
'Name e.g. Microsoft Windows 7 Professional |C:\windows|...
'OSType e.g. 18 / OSArchitecture e.g 64-bit
'Version e.g 6.1.7601 / BuildNumber e.g 7601
winVersion = CInt(Left(objItem.version, 1))
Next
Set objWMIService = Nothing
Set colItems = Nothing
If (winVersion <= 5) Then
' Then we are running XP and can use the original mechanism
Set cd = CreateObject("UserAccounts.CommonDialog")
cd.InitialDir = initialDir
cd.Filter = "ZIP files|*.zip|Text Documents|*.txt|Shell Scripts|*.*sh|All Files|*.*"
' filter index 4 would show all files by default
' filter index 1 would show zip files by default
cd.FilterIndex = 1
If cd.ShowOpen = True Then
ChooseFile = cd.FileName
Else
ChooseFile = ""
End If
Set cd = Nothing
Else
' We are running Windows 7 or later
Set shell = CreateObject( "WScript.Shell" )
Set ex = shell.Exec( "mshta.exe ""about: <input type=file id=X><script>X.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(X.value);close();resizeTo(0,0);</script>""" )
ChooseFile = Replace( ex.StdOut.ReadAll, vbCRLF, "" )
Set ex = Nothing
Set shell = Nothing
End If
End Function
【问题讨论】:
-
失败怎么办?你有错误吗?
-
失败,因为它不是我想要的,或者它的命令与 win 7 不兼容...
-
并且代码是与批处理文件一起使用的,实际上是一个文本编辑器,我正在制作......对不起,我不够清楚......
-
@lukpoper “失败,因为它不是我想要的......” -- 好的......但是我们怎么知道你在寻找什么?你需要一个清晰的问题陈述。 “这给了我错误 123”或“这不会在应该创建文件的时候创建文件”。
标签: batch-file vbscript