Skip pauses when running a batch script from Task Scheduler

COUPLE QUICK WAYS TO SKIP BATCH FILE LOGIC FROM RUNNING

TO START: I'm not familiar with any ways to skip PAUSE commands in a batch script (at least in your case to meet your requirements) unless you use one of the two solution I'll list below in thorough detail. I know of no switches such as --skippauses that would accomplish that with such a simple solution in calling or executing a batch script that would just work to skip any or all PAUSE commands without changing the actual logic of the batch script beforehand to potentially accept that as an argument to then NOT do a PAUSE command, but you would HAVE to make changes to the batch script logic in order for it to work that way.

SOLUTION 1 (does not change called batch script file that you call)

Dynamically build a new dynamically built script MATCHING the logic of the the CALLed script and EXCLUDE all lines containing the PAUSE string (case insensitive), and then run the dynamically built script instead with same logic as the CALLed script minus all LINES containing PAUSE, and thus any PAUSE commands.

Just add something similar to your myBatch.bat as my below example and be sure to point directories, etc where those need to be. Essentially, this will run the FINDSTR /i /v command against the script you CALL, and then read it line-by-line, but EXCLUDE all lines containing the string PAUSE which is NOT case sensitive, and pipe that right over to a new dynamically built batch script with a unique name that you then run your CALL against.

EXAMPLE SCRIPT

@ECHO OFF

:: Parse out space from hour part of time and replace with a zero (for hours of 00:00 - 09:59)
SET hh=%TIME:~0,2%
SET hh=%hh: =0%
SET DateString=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%hh%%TIME:~3,2%%TIME:~6,2%
SET DynScriptName=%Temp%\NOPAUSE_%~N0_%DateString%.cmd

:: If DynScriptName already exists, just delete it
IF EXIST "%DynScriptName%" DEL /F /Q "%DynScriptName%"

:: Use FINDSTR to ignore all lines containing "pause" but pipe all other lines to DynScriptName to later CALL/EXECUTE
:: /i switch denotes case insensitivity, /v switch denotes exclude value in results piped to DynScriptName
FINDSTR /i /v "pause" "C:\folder\folder\myBatch.bat">"%DynScriptName%"

:: Call the dynamically built script without PAUSE command lines
CALL "%DynScriptName%"

:: If DynScriptName already exists, just delete it (post cleanup)
IF EXIST "%DynScriptName%" DEL /F /Q "%DynScriptName%"  
GOTO EOF

CONCLUSION: This (Solution 1)should satisfy your two needs in particular:

  1. You have a way to now run a dynamically built script without PAUSE commands based on the running a script that dynamically builds a new script against that scripts [contents] logic, per run, but to exclude all PAUSE containing lines and thereby eliminating the PAUSE commands from running.

  2. Also, this ensure the current script which you are running via CALL DOES NOT need to be modified to accommodate for the requested changes in your inquiry as it just dynamically rebuilds another excluding what you requested.

  3. Furthermore, another simple solution as in Solution 2 (below) gives a method to indeed make changes to the script you CALL, if you want to also consider this easy route, you have my Solution 2 example as well as the other articles for changes needed at that level.


SOLUTION 2 (does change called batch script logic)

In your script with the PAUSE command, you'd put a label on it like the below for example. You'd then put a label on the logic just after the PAUSE command so you can add IF logic to look for the fist argument passed to the script when it's executed.

Just above the PAUSE command label (meaning :PauseLabel in my example), you'd put the IF [%1]==[] GOTO PauseLabel and then the very next line beneath that you'd put GOTO SkipPause.

This tells it if the script was executed and NO first argument what passed, then go to :PauseLabel and do what's there, which is PAUSE. If there is ANY first argument passed, then it will go to :SkipPause and do what's next, which is the rest of your logic without the PAUSE command.

This way people can run it all day long manually by just double-clicking it (not passing it an argument), but your scheduled job can pass any first argument you want (it's just a dummy argument) to ensure the IF logic can just confirm it exists or if it's NULL.

Script with PAUSE command 1st argument check and skip logic example below

<Script logic before PAUSE>

IF [%1]==[] GOTO PauseLabel
GOTO SkipPause

:PauseLabel 
 PAUSE

:SkipPause
 <Rest of your batch logic>

Passing the argument example to your script (where it's scheduled)

myBatch.bat SkipPause

Please note that when you schedule the batch the first argument can be SkipPause, dummy, DosEquis or anything else you want it to be. The logic with the IF just says, "is there an argument passed to the script when it was executed, and if so, go to :SkipPause, if not, go to :PauseLabel.

Tags:

Windows 7