简体   繁体   English

如何在Windows后台静默执行批处理文件

[英]How to execute batch file silently in windows background

i have a batch file which helps to start my rails server.when i am starting my batch file the command prompt is opening but here i need the cmd should not visible to user or it will execute at windows background.I am explaining mt .bat file code below. 我有一个有助于启动我的Rails Server的批处理文件。当我启动我的批处理文件时,命令提示符正在打开,但是在这里我需要cmd对用户不可见,否则它将在Windows background中执行。我在解释mt .bat文件代码如下。

c:
cd c:\\Site\swargadwara_puri

rails server

Please help me. 请帮我。

You could run it silently using a Vbscript file instead. 您可以改为使用Vbscript文件以静默方式运行它。 The Run Method allows you running a script in invisible mode. 运行方法允许您以不可见模式运行脚本。 Create a .vbs file like this one : 创建一个像这样的.vbs文件:

Option Explicit
Dim MyBatchFile
MyBatchFile = "C:\New Floder\toto 1.bat"
Call Run(MyBatchFile,1,False) 'Showing the console
Call Run(MyBatchFile,0,False) 'Hidding the console
'*********************************************************************************
Function Run(MyBatchFile,Console,bWaitOnReturn)
    Dim ws,Result
    Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
    If Console = 0 Then
        Result = ws.run(DblQuote(MyBatchFile),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
        Result = ws.run(DblQuote(MyBatchFile),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
'*********************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************

The second argument in this example sets the window style. 本示例中的第二个参数设置窗口样式。 0 means "hide the window, and 1 means "show the window" 0表示“隐藏窗口,1表示“显示窗口”

Complete syntax of the Run method : Run方法的完整语法:

 object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

Arguments: 参数:

  • object: WshShell object. 对象:WshShell对象。
  • strCommand: String value indicating the command line you want to run. strCommand:字符串值,指示要运行的命令行。 You must include any parameters you want to pass to the executable file. 您必须包括要传递给可执行文件的所有参数。
  • intWindowStyle: Optional. intWindowStyle:可选。 Integer value indicating the appearance of the program's window. 指示程序窗口外观的整数值。 Note that not all programs make use of this information. 请注意,并非所有程序都使用此信息。
  • bWaitOnReturn: Optional. bWaitOnReturn:可选。 Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. 布尔值,指示脚本在继续执行脚本中的下一条语句之前是否应等待程序完成执行。 If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. 如果设置为true,则脚本执行将暂停,直到程序完成为止,然后Run将返回程序返回的所有错误代码。 If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code). 如果设置为false(默认值),则Run方法在启动程序后立即返回,并自动返回0(不会被解释为错误代码)。

您可以最小化批处理命令,例如使用:

START /MIN rails server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM