如何运行 Windows 批处理文件但隐藏命令窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a windows batch file but hide the command window?
提问by barfoon
How can I run a windows batch file but hiding the command window? I dont want cmd.exe to be visible on screen when the file is being executed. Is this possible?
如何运行 Windows 批处理文件但隐藏命令窗口?我不希望 cmd.exe 在执行文件时在屏幕上可见。这可能吗?
回答by Oleg
If you write an unmanaged programand use CreateProcessAPI then you should initialize lpStartupInfo
parameter of the type STARTUPINFOso that wShowWindow
field of the struct is SW_HIDEand not forget to use STARTF_USESHOWWINDOW
flag in the dwFlags
field of STARTUPINFO. Another method is to use CREATE_NO_WINDOWflag of dwCreationFlags
parameter. The same trick work also with ShellExecuteand ShellExecuteExfunctions.
如果您编写非托管程序并使用CreateProcessAPI,那么您应该初始化STARTUPINFOlpStartupInfo
类型的参数,以便结构的字段是SW_HIDE并且不要忘记在STARTUPINFO字段中使用标志。另一种方法是使用参数的CREATE_NO_WINDOW标志。同样的技巧也适用于ShellExecute和ShellExecuteEx函数。wShowWindow
STARTF_USESHOWWINDOW
dwFlags
dwCreationFlags
If you write a managed applicationyou should follows advices from http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx: initialize ProcessStartInfo
with CreateNoWindow = true
and UseShellExecute = false
and then use as a parameter of . Exactly like in case of you can set property WindowStyle
of ProcessStartInfo
to ProcessWindowStyle.Hidden
instead or together with CreateNoWindow = true
.
如果你写一个托管的应用程序,你应该从如下意见http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx:初始化ProcessStartInfo
与CreateNoWindow = true
和UseShellExecute = false
,然后作为一个参数使用。就像您可以将属性设置WindowStyle
为ProcessStartInfo
toProcessWindowStyle.Hidden
或与CreateNoWindow = true
.
You can use a VBS scriptwhich you start with wcsript.exe. Inside the script you can use CreateObject("WScript.Shell")
and then Runwith 0 as the second (intWindowStyle
) parameter. See http://www.robvanderwoude.com/files/runnhide_vbs.txtas an example. I can continue with Kix, PowerShelland so on.
您可以使用以 wcsript.exe 开头的 VBS 脚本。在脚本中可以使用CreateObject("WScript.Shell")
,然后运行作为第二(0 intWindowStyle
)参数。以http://www.robvanderwoude.com/files/runnhide_vbs.txt为例。我可以继续使用 Kix、PowerShell等。
If you don't want to write any program you can use any existing utilitylike CMDOW /RUN /HID "c:\SomeDir\MyBatch.cmd", hstart /NOWINDOW /D=c:\scripts "c:\scripts\mybatch.bat", hstart /NOCONSOLE "batch_file_1.bat"which do exactly the same. I am sure that you will find much more such kind of free utilities.
如果您不想编写任何程序,您可以使用任何现有的实用程序,例如CMDOW /RUN /HID "c:\SomeDir\MyBatch.cmd", hstart /NOWINDOW /D=c:\scripts "c:\scripts\mybatch .bat", hstart /NOCONSOLE "batch_file_1.bat"完全一样。我相信您会发现更多此类免费实用程序。
In some scenario (for example starting from UNC path) it is important to set also a working directoryto some local path (%SystemRoot%\system32
work always). This can be important for usage any from above listed variants of starting hidden batch.
在某些情况下(例如从 UNC 路径开始),将工作目录设置为某个本地路径(%SystemRoot%\system32
始终工作)也很重要。这对于使用上面列出的启动隐藏批处理的任何变体都很重要。
回答by BitKFu
Using C# it's very easy to start a batch command without having a window open. Have a look at the following code example:
使用 C#,无需打开窗口即可轻松启动批处理命令。看看下面的代码示例:
Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "doSomeBatch.bat";
process.Start();
回答by rain
For any executable file, you can run your program using cmd with "c" parameter:
对于任何可执行文件,您都可以使用带有“c”参数的 cmd 运行您的程序:
cmd /c "your program address"\"YourFileName".bat
(->if it's a batch file!) As a final solution, I suggest that you create a .cmd file and put this command in it:
(->如果是批处理文件!)作为最终解决方案,我建议您创建一个 .cmd 文件并将此命令放入其中:
cmd /c "your program address"\"YourFileName".bat
exit
Now just run this .cmd file.
现在只需运行这个 .cmd 文件。
回答by npocmaka
HereI've compiled all ways that I know to start a hidden process with batch without external tools.With a ready to use scripts (some of them rich on options) , and all of them form command line.Where is possible also the PID is returned .Used tools are IEXPRESS,SCHTASKS,WScript.Shell,Win32_Process and JScript.Net - but all of them wrapped in a .bat
files.
在这里,我编译了所有我知道的无需外部工具即可使用批处理启动隐藏进程的方法。准备好使用脚本(其中一些选项丰富),并且所有这些都形成命令行。PID 在哪里也可能返回。使用的工具是 IEXPRESS、SCHTASKS、WScript.Shell、Win32_Process 和 JScript.Net - 但它们都包含在一个.bat
文件中。
回答by Billy ONeal
Native C++ codified version of Oleg's answer -- this is copy/pasted from a project I work onunder the Boost Software License.
Oleg 答案的本机 C++ 编码版本——这是从我在 Boost 软件许可证下工作的项目中复制/粘贴的。
BOOL noError;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
noError = CreateProcess(
NULL, //lpApplicationName
//Okay the const_cast is bad -- this code was written a while ago.
//should probably be &commandLine[0] instead. Oh, and commandLine is
//a std::wstring
const_cast<LPWSTR>(commandLine.c_str()), //lpCommandLine
NULL, //lpProcessAttributes
NULL, //lpThreadAttributes
FALSE, //bInheritHandles
CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT, //dwCreationFlags
//This is for passing in a custom environment block -- you can probably
//just use NULL here.
options.e ? environment : NULL, //lpEnvironment
NULL, //lpCurrentDirectory
&startupInfo, //lpStartupInfo
&processInformation //lpProcessInformation
);
if(!noError)
{
return GetLastError();
}
DWORD exitCode = 0;
if (options.w) //Wait
{
WaitForSingleObject(processInformation.hProcess, INFINITE);
if (GetExitCodeProcess(processInformation.hProcess, &exitCode) == 0)
{
exitCode = (DWORD)-1;
}
}
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
回答by badbod99
This little VBScript from technetdoes the trick:
这个来自technet 的小 VBScript可以解决这个问题:
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("mybatch.bat", null, objConfig, intProcessID)
Edit mybatch.bat
to your bat file name, save as a vbs, run it.
编辑mybatch.bat
成你的 bat 文件名,另存为 vbs,运行它。
Doc says it's not tested in Win7, but I just tested it, it works fine. Won't show any window for whatever process you run
Doc 说它没有在 Win7 中测试,但我只是测试了它,它工作正常。不会为您运行的任何进程显示任何窗口
回答by Chris O
You could write a windows service that does nothing but execute your batch file. Since services run in their own desktop session, the command window won't be visible by the user.
您可以编写一个只执行批处理文件的 Windows 服务。由于服务在它们自己的桌面会话中运行,因此用户将看不到命令窗口。
回答by Papa Dilbert
Create a shortcut to your bat
file by using the right-click
and selecting Create shortcut
.
Right-click
on the shortcut you created and click on properties.
Click on the Run drop-down box and select Minimized.
bat
使用right-click
并选择来创建文件的快捷方式Create shortcut
。
Right-click
在您创建的快捷方式上,然后单击属性。单击运行下拉框并选择最小化。
回答by u01jmg3
Use Bat To Exe Converterand compile the Bat file as an executable.
使用Bat To Exe Converter并将 Bat 文件编译为可执行文件。
Steps:
脚步:
- Open Bat to Exe Converter
- Select your Bat file
- In the options select "Invisible Application"
- Finish by pressing the compile button
- 打开 Bat to Exe 转换器
- 选择您的 Bat 文件
- 在选项中选择“隐形应用程序”
- 按编译按钮完成
回答by Anuroop Vincent
1,Download the bat to exe converter and install it 2,Run the bat to exe application 3,Download .pco images if you want to make good looking exe 4,specify the bat file location(c:\my.bat) 5,Specify the location for saving the exe(ex:c:/my.exe) 6,Select Version Information Tab 7,Choose the icon file (downloaded .pco image) 8,if you want fill the information like version,comapny name etc 9,change the tab to option 10,Select the invisible application(This will hide the command prompt while running the application) 11,Choose 32 bit(if you select 64 bit exe will work only in 32 bit OS) 12,Compile 13,Copy the exe to the location where bat file executed properly 14,Run the exe
1、下载bat转exe转换器并安装 2、运行bat转exe应用程序 3、下载.pco图片如果你想制作好看的exe 4、指定bat文件位置(c:\my.bat) 5、指定保存 exe 的位置(例如:c:/my.exe) 6、选择版本信息选项卡 7、选择图标文件(下载的 .pco 图像) 8、如果要填写版本、公司名称等信息 9 ,将选项卡更改为选项 10,选择不可见的应用程序(这将在运行应用程序时隐藏命令提示符) 11,选择 32 位(如果您选择 64 位 exe 将仅适用于 32 位操作系统) 12,编译 13,复制exe到bat文件正确执行的位置 14、运行exe