Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

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).

share|improve this answer
1  
superuser.com/questions/345602/… also covers this issue –  K7AAY Jan 10 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 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. –  user1776193 Jan 10 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 –  user1776193 Jan 16 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 at 16:19

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.

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

I can't comment - reputation. 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 &

share|improve this answer

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.