Article directory
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.bat
test 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 VBScript
the script
You can write a VBScript
script file (for example hiderun.vbs
) and use that script to run .bat
the file. In VBScript
, you can use the method Shell
of the object Run
to run .bat
the 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.exe
the absolute path of the program.
Then by double-clicking hiderun.vbs
(or running the script in other programs vbs
), it test.bat
will 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 mshta
calls js
or vbs
scripts
It can be bat
directly mshta
called js
or vbs
scripted in the original batch, and the modification test.bat
is 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
%0
Represents the currently running batch file name, %~nx0
which will be %0
expanded to the file name and extension, that is, the absolute path of the currently running batch. For Wscript.Shell
more 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 bat
compiled into exe
a program
Use a third-party tool to .bat
convert the file into .exe
an executable program file, such as using Bat To Exe Converter
a 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
Note: 选项
- exe格式
Set to 32-bit or 64-bit invisible before converting to xxx.exe
file.
Then double-click the generated xxx.exe
program (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
添加参数
起始于
常规
安全选项
不管用户是否登录都要运行
只有在用户登录时
Summarize:
- Solution 1, built-in VBScript, easy to use
- Solution 2, it will flash when running bat, other solutions do not have this problem
- Solution 3, you need to download a third-party tool, convert bat to exe and execute it
- 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