2016-12-06 48 views
1

我在问你们如何运行.vbs中的.bat文件,而无需指定其包含的驱动器C:。我想尝试进行某种安装,然后我做了一个假装载或安装屏幕,以便.bat文件打开并从.vbs开始执行其小型动画。关于这个问题是当我发送这个.vbs.bat它总是说发生了一个错误;这是我发现:从vbs运行.bat文件而无需指定其驱动器C:

CreateObject("WScript.Shell").Run("""C:\Users\Name\Desktop\Thingy.bat""") 

与该脚本的问题是,我把它的人也需要相同的名称,我和C:。我该如何改变这个脚本,或者它不可能?

+0

如果它总是出现在桌面上,您可以使用'%USERPROFILE%'或与vbs中相同的代码... – geisterfurz007

+2

将脚本路径获取到变量中。 'scriptPath = FSO.GetParentFolderName(wscript.ScriptFullName)' – Squashman

回答

0

下一页评论代码段应做的工作(大多数语句和命令逐项可理解的缘故):

option explicit 
On Error GoTo 0 

    ' declare variables 
Dim WshShell, sUserProfile, strCommand, fso 

    ' assign all object references to appropriate variables 
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set fso  = CreateObject("Scripting.FileSystemObject") 

    ' get an environment variable's expanded value 
sUserProfile = WshShell.ExpandEnvironmentStrings("%UserProfile%") 

    ' build command to run 
strCommand = """" _ 
     & fso.BuildPath(fso.BuildPath(sUserProfile, "Desktop"), "Thingy.bat") & """" 

Wscript.Echo "(debug) command to run" & vbNewLine & strCommand 

WshShell.Run strCommand 
0

如果批处理文件始终在桌面上,你应该使用% USERPROFILE%像这样:

CreateObject("WScript.Shell").Run(chr(34) & "%userprofile%\Desktop\Thingy.bat" & chr(34)) 

或者类似的东西,当批处理文件和VBScript文件都在同一个文件夹:

Set FSO = CreateObject("Scripting.FileSystemObject") 
scriptPath = FSO.GetParentFolderName(wscript.ScriptFullName) 
wscript.echo scriptPath 
MyBatchFile = chr(34) & scriptPath & "\Thingy.bat" & chr(34) 
wscript.echo MyBatchFile 
CreateObject("WScript.Shell").Run(MyBatchFile)