0

I am use a windows command prompt for hours a day at work, and I want to spice things up. I want my text color to change color every 1 second. I have a batch script that does this.

@echo off
set NUM=0 1 2 3 4 5 6 7 8 9 A B C D E F
for %%x in (%NUM%) do ( 
    for %%y in (%NUM%) do (
        color %%x%%y
        timeout 1 >nul
    )
)

It nicely changes the color of my foreground or background every second. However, I want to have this batch script run automatically whenever I open my command prompt shortcut. So in my Shortcut properties, I set the "Target" to this:

Shortcut Target:

C:\Windows\system32\cmd.exe /k auto-change-text-color.bat

This works as expected, but because the batch file is running, I cannot use the command prompt. And instead I just see the background color changing once in a while.

Is there any way to run this in the background and continue using the command prompt while the colors change?

If not, is there any other way to do what I want to do? Maybe with Java, perl, or something besides a batch file?

Thank you for your help.

4 Answers 4

1

It is a bit tricky, because you do not want the background color changing process to intercept any of your console input. You could add the /NOBREAK option to the TIMEOUT command, such that the script never reads stdin, except it still could process a <CTRL-Break> interupt, and ask if you want to terminate the script. The user could respond with Y and terminate the background process, but that could get confusing if the main process also has a running batch script. Which process gets the input?

You can redirect stdin to nul for the background process, but TIMEOUT fails if input is redirected.

I modified your auto-change-text-color.bat to use a PING delay instead of TIMEOUT, thus allowing stdin to be redirected to NUL. I also put one last COLOR command at the end to finish with a more readable color combination.

@echo off
set NUM=0 1 2 3 4 5 6 7 8 9 A B C D E F
for %%x in (%NUM%) do (
  for %%y in (%NUM%) do (
    color %%x%%y
    >nul ping localhost -n 2
  )
)
color 07

The following command is used to launch the script in the background with stdin redirected to nul:

start "" /b cmd /c "auto-change-text-color.bat <nul"

The only odd side effect is that the background process still can receive <CTRL-Break>, and it prints out the message Terminate batch job (Y/N)?, but immediately continues without waiting for input. So you can safely start a batch script in the main process, issue <CTRL-Break> and get two Terminate messages, and press Y or N knowing that only the batch script in your main process will respond (terminate or not).

5
  • 1
    superuser.com/questions/345602/… also covers this issue
    – K7AAY
    Jan 10, 2014 at 19:03
  • @K7AAY - Ooh, interesting. I wasn't aware that <Ctrl-C> is disabled for background process. I updated my answer to to reflect that only <Ctrl-Break> works.
    – dbenham
    Jan 10, 2014 at 19:13
  • @dbenham thank you very much for this. I was able to complete it to my liking thanks to you. I made an answer of my own to detail my final batch file and command. Jan 10, 2014 at 23:50
  • @dbenham Can you think of a way for me to configure the script, or do this in a way that I can stop or pause the script (color changing), and maybe even start it back up again? Stopping it would be enough, but starting it back up again would be double awesome. Thanks again for everything. Been loving this! i.minus.com/iZC4WapYtRVad.gif Jan 16, 2014 at 16:01
  • @user1776193 - Very simple :-) One small change to your loop: if not exist "somePath\pauseColor.signal" color%%X%%Y. Now you can pause by creating the file, and resume by deleting the file.
    – dbenham
    Jan 16, 2014 at 16:19
1

The following command will do what you want:

start /b "" "%COMSPEC%" /c auto-change-text-color.bat

It will start an instance of CMD.EXE (or whatever your COMSPEC is) running in the background of the active console window, executing auto-change-text-color.bat until the batch file ends, at which time the background CMD.EXE will terminate.

2
  • Close, but there are issues with the background process intercepting console input. See my answer
    – dbenham
    Jan 10, 2014 at 19:00
  • Thank you @Woody. I got to work right away when I saw your answer. Jan 10, 2014 at 23:51
0

Thank you @dbenham and @Woody for your help. Using your answers I was able to do exactly what I wanted to do.

I ended up using this as my auto-change-text-color.bat command, because I ended up wanting just the text to change, wanted it to go on forever, and got rid of some colors that didn't look well with a white (F) background:

@echo off
:loop
set NUM=0 1 2 3 4 5 6 8 9 A B C
for %%x in (%NUM%) do ( 
    color F%%x
    >nul ping localhost -n 4
)
goto loop

As for my Windows Command Prompt shortcut, it starts in my development directory, and the "Target" is:

%COMSPEC% /t:F9 /s /k cd scripts && start /b %COMSPEC% /c auto-change-text-color.bat <nul && cd .. && env

It opens up in my development environment's script directory, runs the color changing script in the background that is stored in my scripts folder, then goes back to the parent folder and runs another batch file setting all my environment variables. It's perfect. Thank you again!

-1

In Linux, the & (ampersand) is used to run a background task. I have no idea on Win, but try C:\Windows\system32\cmd.exe /k auto-change-text-color.bat &

2
  • You're right, you have no idea on Windows. & doesn't do what it does on Unix, so this answer is useless. See ss64.com/nt/syntax-redirection.html
    – DavidPostill
    Jun 15, 2016 at 16:20
  • This doesn't work. If you have no idea on Windows, why did you post this as an answer? Jan 13, 2017 at 2:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .