Several schemes for Windows bat to hide the running window

1. Background

When some programs execute batch scripts, they may see a dos window, or see the window flash by. If there is no need to interact with the user during the execution of the batch script, in order to improve the user experience and prevent user misoperation, closing the running batch dos window and causing some problems, it is recommended to put the batch in the background (hidden )run.
Next, I will summarize the ways to hide the window when the bat is running. (minimizing the window is not discussed here)

2. Test data

In order to verify that the bat batch is indeed running in the background instead of running in the dos window, use the following test.battest script

@echo off
echo [%time%]: batch is running.
ping -n 11 127.0.0.1 > run_res.txt

If the batch process is not running in the background, you will see the dos window stay for about 10 seconds, you can judge whether the dos window is successfully running in the background through the generated run_res.txt file

3. Hide the bat running window scheme

1. Use VBScriptthe script

You can write a VBScriptscript file (for example hiderun.vbs) and use that script to run .batthe file. In VBScript, you can use the method Shellof the object Runto run .batthe file, and set the window style to not display.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "%comspec% /c test.bat", 0

%comspec%It is a system environment variable, and its value is cmd.exethe absolute path of the program.
Then by double-clicking hiderun.vbs(or running the script in other programs vbs), it test.batwill run in the background (the window will not flash).

You can also call hiderun.vbs in another bat batch, the calling method is as follows, but running this bat batch will flash for a while:

@echo f
::cscript解释器
cscript HideRun.vbs //nologo
::wscript解释器
wscript HideRun.vbs //nologo

2. Use mshtacalls jsor vbsscripts

It can be batdirectly mshtacalled jsor vbsscripted in the original batch, and the modification test.batis as follows:

@echo off
if "%1"=="h" goto begin
start mshta vbscript:createobject("wscript.shell").run("%~nx0"^&" h",0)^&(window.close) && exit
::start mshta "javascript:new ActiveXObject('WScript.Shell').Run('%~nx0 h',0);window.close();" && exit
:begin
::以下为正常批处理命令,不可含有pause set/p等交互命令
echo [%time%]: batch is running.
ping -n 10 127.0.0.1 > run_res.txt

%0Represents the currently running batch file name, %~nx0which will be %0expanded to the file name and extension, that is, the absolute path of the currently running batch. For Wscript.Shellmore description of the object, please refer to: Wscript.Shell Object Details

It should be noted that running the bat batch in this way will flash for a while.

3. Will be batcompiled into exea program

Use a third-party tool to .batconvert the file into .exean executable program file, such as using Bat To Exe Convertera tool, download address: https://soft.3dmgame.com/down/202761.html
If you don’t use the official website, you know it.
After downloading and decompressing, click Bat_To_Exe_Converter.exe, Open and use directly
bat to exe

Note: 选项- exe格式Set to 32-bit or 64-bit invisible before converting to xxx.exefile.
Then double-click the generated xxx.exeprogram (or run this exe program in other programs) to achieve the effect of running in the background.
​This method will not be deleted when running, and it will run completely in the background.

4. use任务计划程序

Create a timed task, I will not go into details on how to create it. There are two points that need to be emphasized : 1) Fill in the full path of the batch process that needs to be run in the
task 操作settings , and fill in as needed; 2) In the - of the task , you must select it , and the task can only run in the background when the bat batch process is executed. (If you choose the option, the dos window will still pop up, I don't understand why, my operating system is Win10)程序或脚本test.bat添加参数起始于
常规安全选项不管用户是否登录都要运行只有在用户登录时
Runs whether the user is logged in or not

Summarize:

  1. Solution 1, built-in VBScript, easy to use
  2. Solution 2, it will flash when running bat, other solutions do not have this problem
  3. Solution 3, you need to download a third-party tool, convert bat to exe and execute it
  4. Scenario 4, scheduled tasks are more commonly used, especially for tasks that need to be performed regularly


Reference:
Hide cmd window
Wscript.Shell object details when running bat

Guess you like

Origin blog.csdn.net/B11050729/article/details/131711612