问题
I want to start a batch file in a hidden way.
This discussion describes very well how it can be done: by using the CreateProcess API function. Using this method my started batch script is not visible.
The problem is though that the programs (exes) called inside of the batch are hidden, too!
My goal is to hide the started batch windows but show the windows of the applications called inside the batch.
Is it possible? If so, how? Can I use the CreateProcess function for it or do I need another one?
回答1:
Next commented batch script shows possible approach to run visibly
- another batch script:
cliParser.batsimply lists all supplied parameters;cliParseArg.batdoes the same and setserrorlevelto parameters count (zero-based so never returs0) byexit /b %i%;
- a GUI application: exercised common
calcandnotepadexecutables; - a Win32 console application:
cliParser.exeiscliParser.batrewritten inC++;
from within an invisible command line window. Examples given how-to run above asynchronously as well as synchronously (i.e. waiting until called app ends).
@ECHO OFF
SETLOCAL EnableExtensions
rem STDOUT output is invisible if this script runs in a hidden window
wmic OS get LocalDateTime
rem another batch script asynchronously => visible
start "batch asyn" cmd /K ""D:\bat\cliParser.bat" par1 %* "par 3""
rem errorlevel clear: by virtue of Dave Benham in reply to http://superuser.com/a/649329/376602
(call )
rem another batch script synchronously => visible
start "batch synchr" /WAIT cmd /K "("D:\bat\cliParseArg.bat" par1 %* "par 3"&pause&exit /B %%%%errorlevel%%%%)"
rem `pause` command in above line is merely for debugging purposes to ensure that window is visible
echo cliParseArg.bat parameter count=%errorlevel%
echo(
rem a GUI application asynchronously => visible
start "" calc.exe
rem a GUI application synchronously => visible
notepad.exe
rem a console application asynchronously => visible
start "CApp asyn" cmd /K ""D:\bat\cliParser.exe" rap1 %* "rap 3""
rem a console application synchronously => visible
start "CApp synchr" /WAIT cmd /K "("D:\bat\cliParser.exe" arg1 %* "arg 3"&exit /B)"
rem STDOUT output is invisible if this script runs in a hidden window
cmd /C wmic OS get LocalDateTime
Output if called from a visible cmd window first and then in a hidden window::
==> D:\bat\SO\37181230.bat "xx yy"
LocalDateTime
20160512195221.499000+120
cliParseArg.bat parameter count=4
LocalDateTime
20160512195244.958000+120
==> Wscript D:\VB_scripts\runhidden.vbs "D:\bat\SO\37181230.bat" "A1 B2"
==>
The runhidden.vbs is adapted from Rob van der Woude's script where
strArguments = strArguments & " " & WScript.Arguments(i)line was changed tostrArguments = strArguments & " """ & WScript.Arguments(i) & """"
回答2:
This should do the trick, use this code in your batch file to start your EXEs.
@echo off
title GEO-START v2
REM #############################################################
REM ## GEO-START ## starts any file any location with variables. ##
REM #############################################################
REM ##
REM "start-and-wait-yes-no" variable, Y is yes, N is no.
REM ##
REM "var" variable, "/silent" or "/y". ## The /y variable is grait for "ms self extracting cabinet files" CAB.exe.
REM ##
REM "silent" variable, 0 will set the "var" variable to true and trys to run hidden.
REM "silent" variable, 1 will set the "var" variable to false.
REM ##
REM "GEO-START" variable, the name and location of the file you want to start.
REM ################################################
set Y=true
set N=Wscript.Quit
REM ##############
REM #####################
REM ## variables to set ##
set start-and-wait-yes-no=%Y%
set var=/silent
set silent=1
set GEO-START=Winamp3 v9.1.exe
echo Set WshShell = WScript.CreateObject("WScript.Shell") > GEO-START.vbs && echo WSHShell.Run chr(34) + "%GEO-START%" + chr(34) + " %var%",%silent%,%start-and-wait-yes-no% >> GEO-START.vbs && cscript GEO-START.vbs && del GEO-START.vbs
Exit
来源:https://stackoverflow.com/questions/37181230/can-i-run-a-windows-batch-file-with-hidden-cmd-window-but-see-the-programs-start