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

时间:2012-11-27 08:56:20

标签: windows windows-7 batch-processing batch-file

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

我的批次代码是这样的;

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

如何在运行时隐藏此窗口?

4 个答案:

答案 0 :(得分:7)

使用VBScript

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

运行将隐藏批处理文件的内容。

答案 1 :(得分:3)

我不喜欢VBScript解决方案。

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

nircmd.exe win hide ititle "cmd.exe"

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

title MyBatch
nircmd.exe win hide ititle "MyBatch"

答案 2 :(得分:2)

此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

答案 3 :(得分:0)

我知道帖子已经过时了,但这是我的解决方案,来自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...