windows 运行 .bat 文件时如何隐藏 ms-dos 窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13580474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:04:46  来源:igfitidea点击:

How can I hide ms-dos window when running a .bat file?

windowswindows-7batch-processingbatch-file

提问by

I am running a .bat file for my script (Scheduled Tak (CronJob)) per minute. When it runs, windows command prompt appears for a fiction of time.

我每分钟为我的脚本 (Scheduled Tak (CronJob)) 运行一个 .bat 文件。当它运行时,windows 命令提示符会出现一段时间。

My batch code like this;

我的批处理代码是这样的;

@ECHO OFF
C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\tst\index.php" 

How can I hide this window when it run?

运行时如何隐藏此窗口?

采纳答案by Bali C

Use a VBScript

使用 VBScript

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\yourbatch.bat"), 0, True

Run that which will run your batch file hidden.

运行它将隐藏您的批处理文件。

回答by BijaN-R

I don't like VBScript solution.

我不喜欢 VBScript 解决方案。

Download and copy nircmd.exeto your %systemroot%\system32folder, then add this command to first line of your batch:

下载nircmd.exe并将其复制到您的%systemroot%\system32文件夹,然后将此命令添加到批处理的第一行:

nircmd.exe win hide ititle "cmd.exe"

or make your batch title custom first with titlecommand to avoid from hiding all cmdwindows, like this:

或者首先使用title命令自定义批处理标题以避免隐藏所有cmd窗口,如下所示:

title MyBatch
nircmd.exe win hide ititle "MyBatch"

回答by zarafsha123

This VBScript creates a copy of your batch file in %Temp%, executes it silently and deletes it afterwards

此 VBScript 在 %Temp% 中创建您的批处理文件的副本,静默执行并随后将其删除

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

Dim tempfolder

Const TemporaryFolder = 2

Dim WshShell, strCurDir

Set WshShell = CreateObject("WScript.Shell")

strCurDir    = WshShell.CurrentDirectory

batch = "@ECHO OFF" & vbCrLf & _
        "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\tst\index.php"

Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)

WshShell.CurrentDirectory = tempfolder

i=1

n=0

While n <> 1

If (fso.FileExists(i&".bat")) Then

  i = i + 1

Else
  n = 1

End If

Wend

Set File = fso.CreateTextFile(i&".bat",True)

File.Write batch

File.Close

Dim batchfile

batchfile = fso.GetAbsolutePathName(i&".bat")

WshShell.CurrentDirectory = strCurDir

WshShell.Run chr(34) & batchfile & Chr(34), 0, TRUE

fso.DeleteFile batchfile

回答by Jacob

I know the post is old but here is my solution, AGerman from dostips helped me code this script, its very useful.

我知道这篇文章很旧,但这是我的解决方案,来自 dostips 的 AGerman 帮助我编写了这个脚本,它非常有用。

@echo off &setlocal EnableExtensions DisableDelayedExpansion

:: Change the working directory to the directory of the batch file.
:: If the first passed argument was ~e~ (that is, the batch file was called from the VBScript)
::  then shift the parameters by one and continue at label :elevated
cd /d "%~dp0"&if "%~1"=="~e~" (shift&goto :elevated)

:: Assign the passed arguments to variable param.
set "param=%*"

:: NET SESSION fails if the batch code doesn't run with elevated permissions.
::  Assign variable __verb to "open" if the batch file runs elevated or to "runas" if it doesn't run elevated
>nul 2>&1 net session &&(set "__verb=open")||(set "__verb=runas")

:: Assign the name of the VBScript to variable vbs.
:: Assign the full name of the batch file to variable me.
:: Enable delayed variable expansion.
set "vbs=%temp%\uac.vbs"&set "me=%~f0"&setlocal enabledelayedexpansion

:: If arguments were passed, prepare them to be passed from within the VBScript by doubling the quotation marks.
if defined param set "param=!param:"=""!"

:: Write the VBScript. The ShellExecute method will run the batch file in a cmd.exe process where ~e~ will be passed as
::  first argument followed by the original arguments (saved in param). The UAC will be invoked if __verb was set to "runas".
::  Elsewise the UAC will not be invoked. For further information about the ShellExecute method see:
::  https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
>"!vbs!" echo CreateObject("Shell.Application").ShellExecute "!comspec!", "/c """"!me!"" ~e~ !param!""", "", "%__verb%", 0

:: Run the VBScript in a cscript.exe process.
:: Delete the VBScript file.
:: Quit the batch execution.
cscript //nologo "!vbs!"&del "!vbs!"&goto :eof

:elevated
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: Do your elevated stuff here...