Vbscript 如何从后台的命令行执行命令

Vbscript 如何从后台的命令行执行命令,vbscript,cmd,Vbscript,Cmd,我已经编写了vb脚本,从前台的windows命令行运行jar命令 'File paths processFile = "java -jar doSomething.jar C:\folder1\subFolder1 C:\folder2\subFolder2" Set objShell = CreateObject("Wscript.shell") objShell .run "cmd /k CD C:\VBScriptfolder\script" 'Path to vbs

我已经编写了vb脚本,从前台的windows命令行运行jar命令

'File paths
 processFile = "java -jar doSomething.jar C:\folder1\subFolder1 C:\folder2\subFolder2"      

 Set objShell = CreateObject("Wscript.shell")

 objShell .run "cmd /k CD C:\VBScriptfolder\script" 'Path to vbscript which contains command to run from command line
 WScript.Sleep 5000 
 Wait(5)
 objShell .SendKeys processFile 
 WScript.Sleep 3000 
 Wait(3)
 objShell .SendKeys "{ENTER}"
 WScript.Sleep 40000 
 Wait(60)
 objShell .SendKeys "exit{ENTER}"

但我的问题是如何从命令行运行上述命令,这些命令在后台而不是前台执行。

尝试构建您的命令行,并使用wscript.echo进行调试。 而且,如果您认为它是正确的,您应该注释wscript.echo,并取消注释这一行Call Run(StrCmd,0,False)“隐藏控制台”

Option Explicit
Dim StrCmd,Path,processFile
Path = "C:\VBScriptfolder\script" 'Path to vbscript which contains command to run from command line
processFile = "java -jar doSomething.jar C:\folder1\subFolder1 C:\folder2\subFolder2"
StrCmd = "CD /D "& Path & " & " & processFile &""
wscript.echo StrCmd
'Call Run(StrCmd,1,False) 'Showing the console
'Call Run(StrCmd,0,False) 'Hiding the console
'**********************************************************************************************
Function Run(StrCmd,Console,bWaitOnReturn)
    Dim ws,MyCmd,Result
    Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
    If Console = 0 Then
        MyCmd = "CMD /C " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
'A value of 1 to show the MS-DOS console
    If Console = 1 Then
        MyCmd = "CMD /K " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
    Run = Result
End Function
'**********************************************************************************************

你说的“背景”是什么意思?异步?隐藏的?或者两者都有?还是别的?邦德,我是说隐藏不幸的是,你不能在隐藏的窗口上使用
SendKeys()。它必须可见并处于活动状态。不久前,我发布了一个黑客来克服这个问题,但这绝对是一个黑客。它涉及到更改命令提示符窗口的打开位置,该窗口存储在注册表中,以便在屏幕外创建。这肯定会隐藏命令提示符窗口,但我认为OP希望能够向其发送击键。我运行了上面的代码,命令提示符文件名、目录名、,或者卷标语法不正确。您的意思是当它像那样发布时运行它,并且不更改任何设置吗?