51

如何在静默模式下运行 CMD 或 .bat 文件?我希望阻止向用户显示 CMD 界面。

4

11 回答 11

48

包括以下短语:

@echo off

就在你的 bat 脚本的顶部。

于 2009-01-04T16:57:41.993 回答
39

我在 StackOverflow 问题中提出了一种在后台运行批处理文件的方法(不显示 DOS 窗口

那应该回答你的问题。

这里是:


从您的第一个脚本中,使用以下行调用您的第二个脚本:

wscript.exe invis.vbs run.bat %*

实际上,您正在调用一个 vbs 脚本:

  • 你的脚本的 [path]\name
  • 脚本所需的所有其他参数 ( %*)

然后,invis.vbs 将使用Windows Script Host Run() 方法调用您的脚本,该方法需要:

  • intWindowStyle : 0 表示“不可见的窗口”
  • bWaitOnReturn : false 表示您的第一个脚本不需要等待您的第二个脚本完成

请参阅完整的 invis.vbs 脚本的问题:

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

Tammen 反馈后更新:

如果您在 DOS 会话中并且想要“在后台”启动另一个脚本,那么一个简单的(如前面提到的同一个问题/b中所详述)就足够了:

您可以使用start /b second.bat从您的第一个文件异步启动第二个批处理文件,该文件共享您的第一个窗口。

于 2009-01-04T16:57:52.367 回答
12

我认为这是在不打开 DOS 窗口的情况下运行批处理文件的最简单和最短的解决方案,当您想要安排一组命令定期运行时,它可能会非常分散注意力,因此 DOS 窗口不断弹出,这是您的解决方案. 使用 VBS 脚本调用批处理文件...

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

将上面的行复制到编辑器并使用 .VBS 扩展名保存文件。相应地编辑 .BAT 文件名和路径。

于 2014-01-15T06:18:26.733 回答
8

使用来自http://www.battoexeconverter.com的 Advanced BAT to EXE 转换器

这将允许您将任何其他二进制文件与您的批处理文件嵌入到一个独立的完全无声的 EXE 及其免费软件中

于 2012-11-15T18:26:14.153 回答
7

使用 Bat To Exe 转换器来执行此操作

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

1 - 打开 Bat to Exe Converter,选择您的 Bat 文件。
2 - 在选项菜单中选择“不可见的应用程序”,然后按编译按钮。

完毕!

于 2015-04-15T07:00:35.173 回答
4

尝试SilentCMD。这是一个小型免费软件程序,它执行批处理文件而不显示命令提示符窗口。

于 2011-02-06T13:27:22.347 回答
3

如果我想在静默模式下运行命令提示符,那么有一个简单的 vbs 命令:

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

如果我想在 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"
于 2012-11-26T22:29:13.537 回答
2

我很有信心我最喜欢这种方法。将以下代码复制并粘贴到 .vbs 文件中。从那里您将调用批处理文件...因此请确保您编辑最后一行以指定批处理文件的路径和名称(其中应包含您要启动的文件或执行您需要执行的操作)

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)

它肯定对我有用。欢迎评论:)

于 2013-12-25T03:26:42.030 回答
2

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

@echo off > NUL
于 2019-10-13T17:01:07.400 回答
1

下面的无声 .bat 文件代码可防止需要两个 bat 文件(使用“goto”和“:”)。

它在同一个 .bat 文件中完成所有操作。经过测试并确认在 Windows 10 中工作

确保将“C:\pathToFile\ThisBatFile.bat”替换为同一个 .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

如果您的 .bat 文件需要的不仅仅是“Initialized”参数(它告诉 bat 文件转到 :SilentCall 部分),请添加“ ^& WScript.Arguments(1)、 ”、“ ^& WScript.Arguments(2) , " 等 根据参数的数量,然后编辑调用 wscript.exe 的行:

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

于 2020-09-27T21:32:49.217 回答
0

我创建了RunApp来完成这样的工作,并在我的生产环境中使用它,希望它有所帮助。

如下配置:

文件:config.arg

:style:hidden

MyBatchFile.bat
arg1
arg2

runapp.exe而是启动。

于 2018-06-21T08:19:42.533 回答