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

时间:2014-09-11 09:10:30

标签: 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 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}"

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

1 个答案:

答案 0 :(得分:0)

尝试构建命令行并使用 wscript.echo 进行调试。 并且,如果您认为它是正确的,您应该评论 wscript.echo 并取消注释此行调用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
'**********************************************************************************************