如何在VBScript中获取当前目录并运行文件?

5

我正在尝试查看是否安装了IIS,如果没有安装IIS,则显示一条消息和一个下载exe文件以安装IIS。但是,在vb-script中没有指定完整路径的情况下运行文件很困难。该路径将是动态的,并且除“%cd%”之外,无法指定任何其他目录。

我的代码:

If WScript.Arguments.length =0 Then
Set objShell = CreateObject("Shell.Application")

objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
Dim intCounter, strSubkey
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft"

objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

intCounter=0
For Each subkey In arrSubKeys
If subkey="InetStp" Then
 intCounter=1 or  strSubkey=subkey
End If

Next
currentDirectory = left(WScript.ScriptFullName, Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))

if intCounter=0 then
Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run ("\currentDirectory\noiisinstalled.exe") 
Elseif intCounter=1 then
Wscript.Echo "IIS is Already installed - " & strSubkey
End If
End if

我的问题是运行no iisinstalled.exe文件。无论我尝试什么脚本,都找不到该文件。

2个回答

8
您可以使用Scripting.FileSystemObject获取当前目录。
dim fso: set fso = CreateObject("Scripting.FileSystemObject")

' directory in which this script is currently running
CurrentDirectory = fso.GetAbsolutePathName(".")

要使用此功能来构建新路径,您可以使用BuildPath()函数。

NewPath = fso.BuildPath(CurrentDirectory, "noiisinstalled.exe")

1
Set WSHShell = CreateObject("Wscript.Shell")
sCurrentDirectory = WSHShell.CurrentDirectory & "\"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接