53

In a Windows batch file, myScript.bat runs otherScript.bat, and in otherScript.bat, there's a pause in first line.

How can I send a keystroke thast skips the pause in myScript.bat? So my script won't need to take any extract keystroke and won't be blocked by the pause.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Stan
  • 37,207
  • 50
  • 124
  • 185

3 Answers3

80

One way would be to use the echo command to do the keystroke for you.

For example:

myScript.bat

@echo OFF

@echo Calling otherScript.bat...
@echo | call otherScript.bat
@echo Done.

otherScript.bat

pause
@echo Hello World

Stefan's solution is more flexible and allows you to control when to pause or not, but this solution will work if you're not able to revise otherScript.bat for some reason.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • Both answers are awesome, but I can't edit otherScript.bat. So accept this one. – Stan Aug 28 '10 at 01:09
  • Just the solution I was looking for +1 – CraftyFella Jan 24 '13 at 13:01
  • How can i achieve this in vbscript, in mycase "myscript.vbs". If I use the command WshShell.Run " | call otherScript.bat",1,true getting a error that unable to wait for process.. – user1066231 Oct 12 '17 at 06:22
  • for some reason I didn't think that I could pipe TO a .bat file. Now I have to wonder what else I have assumed that could not be done!! – anjanb Jan 14 '18 at 09:51
  • 6
    You should use `@echo. | call otherScript.bat` (note the dot after echo). That way only a new line is sent to the other script instead of the string "ECHO is on." or similar. Sending the string might have negative effect when `set /p` is used in the other script. – 816-8055 Sep 11 '19 at 08:22
21

You could modify otherScript.bat so that it accepts an optional parameter, telling it to skip the pause command like this:

if "%1"=="nopause" goto start
pause
:start
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
1

I would recommend < NUL instead of echo( |

myScript.bat

@echo OFF

@echo Calling otherScript.bat...
set var=One
< NUL call otherScript.bat
@echo Done. var=%var%

otherScript.bat

@echo off
pause
echo Hello World
set var=two

The main difference is, otherscript runs in the same cmd.exe instance, but echo. | call ... will run in a child cmd.exe.

A child instance has the problem, that otherscript can modify environment variables, but the changes are lost after the return

jeb
  • 78,592
  • 17
  • 171
  • 225