CLS (ClearScreen) command not working when .bat file is run from task scheduler

59 views Asked by At

I am trying to build a batch file (Using MS Notepad) to accomplish the following:

  1. Write a message to the console ("Running Task X - Do not interrupt")

  2. Run some commands with the output being written to a log file

  3. Pause

  4. CLEAR THE SCREEN (CLS)

  5. Write a different message to the console ("Running Task Y - Do not interrupt")

  6. Run some more commands with the output being written to the log file

  7. Pause until the screen is manually cleared

When I run my test file directly by double-clicking it, the Clear Screen command works as expected. (of course, the output of steps 2 and 6 will also show on the screen rather than being written to a log file)

When I run the file from the task scheduler, The CLS is ignored and BOTH messages will show on the screen (In this case, the output of steps 2 and 6 is properly directed to a log file)

How can I properly clear the screen when running the file from Task Scheduler?

My batch test file:

@Echo OFF

mode con: cols=85 lines=40

echo ===  This machine is currently performing task "X"   Please do NOT interrupt   === >con

Dir PlaceHolder*

Pause >con

@CLS

echo ===  This machine is currently performing task "Y"   Please do NOT interrupt   === >con

Dir PlaceHolder2*

Pause >con

@Exit
1

There are 1 answers

0
TheAncient On

Turns out, if you only use "@CLS" (as I did), the Clearscreen command gets directed to the LOG rather than to the screen. Since the log CANNOT be cleared, the batch file prepends the next line of output with an upwards arrow and calls it a day. You will need to ensure you explicitly route the Clearscreen command to the console/screen (@CLS >con) in which case it will work as expected.