简体   繁体   English

以静默模式运行 CMD 或 BAT

[英]Running a CMD or BAT in silent mode

How can I run a CMD or .bat file in silent mode?如何在静默模式下运行 CMD 或 .bat 文件? I'm looking to prevent the CMD interface from being shown to the user.我希望防止向用户显示 CMD 界面。

Include the phrase:包括短语:

@echo off

Right at the top of your bat script.就在 bat 脚本的顶部。

I have proposed in StackOverflow question a way to run a batch file in the background ( no DOS windows displayed )我在 StackOverflow 问题中提出了一种在后台运行批处理文件的方法(不显示 DOS 窗口

That should answer your question.那应该回答你的问题。

Here it is:这里是:


From your first script, call your second script with the following line:从您的第一个脚本中,使用以下行调用您的第二个脚本:

wscript.exe invis.vbs run.bat %*

Actually, you are calling a vbs script with:实际上,您正在使用以下命令调用 vbs 脚本:

  • the [path]\\name of your script脚本的 [path]\\name
  • all the other arguments needed by your script ( %* )脚本所需的所有其他参数( %*

Then, invis.vbs will call your script with the Windows Script Host Run() method , which takes:然后,invis.vbs 将使用Windows Script Host Run() 方法调用您的脚本,该方法需要:

  • intWindowStyle : 0 means "invisible windows" intWindowStyle : 0 表示“不可见的窗口”
  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish bWaitOnReturn : false 意味着您的第一个脚本不需要等待您的第二个脚本完成

See the question for the full invis.vbs script:请参阅完整 invis.vbs 脚本的问题:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
                                                         ^
                             means "invisible window" ---| 

Update after Tammen's feedback:在 Tammen 的反馈后更新:

If you are in a DOS session and you want to launch another script "in the background", a simple /b (as detailed in the same aforementioned question ) can be enough:如果您在 DOS 会话中并且想要“在后台”启动另一个脚本,一个简单的/b (如上述相同问题中所述)就足够了:

You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window.您可以使用start /b second.bat从共享第一个窗口的第一个批处理文件异步启动第二个批处理文件。

I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps popping up, here is your solution.我认为这是在不打开 DOS 窗口的情况下运行批处理文件的最简单和最短的解决方案,当您想要安排一组命令定期运行时,它可能会非常分散注意力,因此 DOS 窗口不断弹出,这是您的解决方案. Use a VBS Script to call the batch file ...使用 VBS 脚本调用批处理文件...

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0 
Set WshShell = Nothing 

Copy the lines above to an editor and save the file with .VBS extension.将上面的行复制到编辑器并使用 .VBS 扩展名保存文件。 Edit the .BAT file name and path accordingly.相应地编辑 .BAT 文件名和路径。

Use Advanced BAT to EXE Converter from http://www.battoexeconverter.com使用来自http://www.battoexeconverter.com 的高级 BAT 到 EXE 转换器

This will allow you to embed any additional binaries with your batch file in to one stand alone completely silent EXE and its freeware这将允许您将任何额外的二进制文件与您的批处理文件嵌入到一个独立的完全无声的 EXE 及其免费软件中

Use Bat To Exe Converter to do this使用 Bat To Exe Converter 来做到这一点

http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
(Choose Direct Download Link ) (选择直接下载链接

1 - Open Bat to Exe Converter, select your Bat file. 1 - 打开 Bat to Exe Converter,选择您的 Bat 文件。
2 - In Option menu select "Invisible Application", then press compile button. 2 - 在选项菜单中选择“隐形应用程序”,然后按编译按钮。

Done!完毕!

Try SilentCMD .试试SilentCMD This is a small freeware program that executes a batch file without displaying the command prompt window.这是一个小的免费软件程序,它在不显示命令提示符窗口的情况下执行批处理文件。

If i want to run command promt in silent mode, then there is a simple vbs command:如果我想在静默模式下运行命令提示符,那么有一个简单的 vbs 命令:

Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"

if i wanted to open an url in cmd silently , then here is a code:如果我想在 cmd 中静默打开一个 url ,那么这里是一个代码:

Set WshShell = WScript.CreateObject("WScript.Shell") 
Return = WshShell.Run("iexplore.exe http://otaxi.ge/log/index.php", 0)
'wait 10 seconds
WScript.sleep 10000 
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"

I'm pretty confident I like this method the best.我非常有信心我最喜欢这种方法。 Copy and paste the code below into a .vbs file.将下面的代码复制并粘贴到 .vbs 文件中。 From there you'll call the batch file... so make sure you edit the last line to specify the path and name of the batch file (which should contain the file you'd like to launch or perform the actions you need performed)从那里您将调用批处理文件...因此请确保编辑最后一行以指定批处理文件的路径和名称(其中应包含您想要启动或执行您需要执行的操作的文件)

Const HIDDEN_WINDOW = 12 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set objStartup = objWMIService.Get("Win32_ProcessStartup") 

Set objConfig = objStartup.SpawnInstance_ 
objConfig.ShowWindow = HIDDEN_WINDOW 
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process") 
errReturn = objProcess.Create("C:\PathOfFile\name.bat", null, objConfig, intProcessID)

It definitely worked for me.它绝对对我有用。 Comments are welcomed :)欢迎评论:)

另一种方法,没有 3rd 方程序或转换器(“批处理到 exe”程序实际上只是将您的批处理文件放在 tmp 文件夹中,然后静默运行,这样任何人都可以从那里获取它并获取您的代码)没有 vbs 文件(因为没有人知道 vbs)在批处理文件的开头只有一行。

@echo off > NUL

The below silent .bat file code prevents the need to have two bat files (using "goto" and ":").下面的静默 .bat 文件代码防止需要有两个 bat 文件(使用“goto”和“:”)。

It does it all in the same .bat file.它在同一个 .bat 文件中完成所有操作。 Tested and confirmed working in Windows 10经过测试并确认可在 Windows 10 中工作

Make sure you replace "C:\\pathToFile\\ThisBatFile.bat " with the path to this same .bat file!确保将“C:\\pathToFile\\ThisBatFile.bat”替换为同一个 .bat 文件的路径! Keep the space after ".bat".保留“.bat”后的空格。

@echo off
if [%1]==[] (
    goto PreSilentCall
) else (
    goto SilentCall
)

:PreSilentCall
REM Insert code here you want to have happen BEFORE this same .bat file is called silently
REM such as setting paths like the below two lines

set WorkingDirWithSlash=%~dp0
set WorkingDirectory=%WorkingDirWithSlash:~0,-1%

REM below code will run this same file silently, but will go to the SilentCall section
cd C:\Windows\System32
if exist C:\Windows\Temp\invis.vbs ( del C:\Windows\Temp\invis.vbs /f /q )
echo CreateObject("Wscript.Shell").Run "C:\pathToFile\ThisBatFile.bat " ^& WScript.Arguments(0), 0, False > C:\Windows\Temp\invis.vbs
wscript.exe C:\Windows\Temp\invis.vbs Initialized
if %ERRORLEVEL%==0 (
    echo Successfully started SilentCall code. This command prompt can now be exited.
    goto Exit
)


:SilentCall
cd %WorkingDirectory%
REM Insert code you want to be done silently. 
REM Make sure this section has no errors as you won't be able to tell if there are any, 
REM since it will be running silently. You can add a greater than symbol at the end of
REM your commands in this section to output the results to a .txt file for the purpose 
REM of debugging this section of code.


:Exit

If your .bat file needs more than just the "Initialized" argument (which tells the bat file to go to :SilentCall section), add " ^& WScript.Arguments(1), " , " ^& WScript.Arguments(2), " ,etc.如果你的.bat文件需要不止是“初始化”参数(它告诉bat文件去:SilentCall部分),加上“^&WScript.Arguments(1)”,“^&WScript.Arguments(2) ”等。 depending on the number of arguments, then edit the line where wscript.exe is called:根据参数的数量,然后编辑调用 wscript.exe 的行:

"wscript.exe C:\\Windows\\Temp\\invis.vbs Initialized BatFileArgOne BatFileArgTwo " “wscript.exe C:\\Windows\\Temp\\invis.vbs 初始化BatFileArgOne BatFileArgTwo

I'm created RunApp to do such a job and also using it in my production env, hope it's helps.我创建了RunApp来完成这样的工作,并在我的生产环境中使用它,希望它有所帮助。

The config like below:配置如下:

file: config.arg文件:config.arg

:style:hidden

MyBatchFile.bat
arg1
arg2

And launch runapp.exe instead.而是启动runapp.exe

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

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