简体   繁体   中英

Can I run a windows batch file with hidden cmd window but see the programs started by the batch?

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?

Next commented batch script shows possible approach to run visibly

  • another batch script:
    • cliParser.bat simply lists all supplied parameters;
    • cliParseArg.bat does the same and sets errorlevel to parameters count (zero-based so never returs 0 ) by exit /b %i% ;
  • a GUI application: exercised common calc and notepad executables;
  • a Win32 console application: cliParser.exe is cliParser.bat rewritten in C++ ;

from within an invisible command line window. Examples given how-to run above asynchronously as well as synchronously (ie 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 to
  • strArguments = strArguments & " """ & WScript.Arguments(i) & """"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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