Escape Characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jamessotech
Posts: 3
Joined: 23 Oct 2014 13:24

Escape Characters

#1 Post by Jamessotech » 06 Nov 2014 15:17

I would like to place this line in a batch file, cannot understand how to add escape characters:

echo Esc[1;5;31m Warning You are about to delete files...

I want it to make the text red and blink

any assistance or referral will be appreciated.

Thanks

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Escape Characters

#2 Post by Squashman » 06 Nov 2014 16:32

Not possible!

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Escape Characters

#3 Post by Aacini » 06 Nov 2014 17:21

Copy and paste the text below:

Code: Select all

@echo off
echo  Warning You are about to delete files...

Successfully tested with AnsiSys.exe.

Antonio

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Escape Characters

#4 Post by ShadowThief » 07 Nov 2014 03:13

Squashman wrote:Not possible!

To expand on that slightly, the way you're trying uses ANSI.sys, which doesn't come with modern systems (if you were using Windows 98 or earlier, you wouldn't have a problem).

There are ways to turn the text red, and there are complicated ways to turn just that one line red. I can imagine a way to make the text blink by generating backspace characters and printing with set /p, but I'd have to test it, and it would have to stop blinking before the user provided input.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Escape Characters

#5 Post by foxidrive » 07 Nov 2014 04:18

Aacini wrote:Copy and paste the text below:

Code: Select all

@echo off
echo  Warning You are about to delete files...

Successfully tested with AnsiSys.exe.

Antonio


It's not clear from the above how ansisys will function with the code you have shown, Antonio.

The OP might have trouble figuring it out.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Escape Characters

#6 Post by Yury » 07 Nov 2014 06:44

Code: Select all

@echo off

set "line=Warning! You are about to delete files..."

cd /d "%~dp0"
for /f %%i in ('
 forfiles /m "%~nx0" /c "cmd /c echo 0x08"
') do set BS=%%i
set /p=%BS%<nul>"%line%_"
for /l %%i in (1 1 100) do (
 set /p="Hello, %username%! "<nul
 findstr /a:ac "^" "%line%_*"
 set /p="%BS%  "<nul
 for /l %%j in (1 1 500) do pause<nul>nul
 cls
 )
del "%line%_"

exit /b

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Escape Characters

#7 Post by Squashman » 07 Nov 2014 08:14

@Yury,
I guess that is one way to pseudo do it. But that is more of a flicker then a blink because of the CLS command.

Jamessotech
Posts: 3
Joined: 23 Oct 2014 13:24

Re: Escape Characters

#8 Post by Jamessotech » 07 Nov 2014 09:02

Hi Guys,

Thanks for response. I guess I need to face reality that the old dos days are about gone.

Thanks again

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Escape Characters

#9 Post by Aacini » 07 Nov 2014 10:40

foxidrive wrote:It's not clear from the above how ansisys will function with the code you have shown, Antonio.

The OP might have trouble figuring it out.

You are right, foxi. I understood that the problem was to include the Esc character, and assumed that the OP is using already some Ansi driver...

Jamessotech wrote:Hi Guys,

Thanks for response. I guess I need to face reality that the old dos days are about gone.

Thanks again

Not entirely. You may still use such functionality in several ways. Try this:

  1. Go to AnsiSys post, copy and paste the file "Ansi Color Table.bat" and run it.
  2. Previous Batch file will create a program called AnsiSys.exe
  3. You may use AnsiSys.exe as a filter, that is, you may pipe to it the output of another program with Ansi escape sequences and the output will be displayed the right way (well, just for a few Ansi codes for the moment).

See the example below:


Image


You may also use AnsiSys.exe just in the lines you want to use the Ansi sequences; for example:

Code: Select all

@echo off
echo Normal output
echo Output with Ansi escape sequences | AnsiSys
echo Another normal output...

If you want a more precise emulation of old MS-DOS Ansi escape sequences, look for a program called AnsiCon.sys. However, no one of the Ansi emulators for Windows currently support the blinking of characters; such attribute was changed to light background color.

Antonio

penpen
Expert
Posts: 1995
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Escape Characters

#10 Post by penpen » 08 Nov 2014 09:24

You could use C#.NET Edition (should be installed on systems higher than XP, on XP you have to install it) as a workaround for blinking text.
Note: I've created it some time ago and actually it is not finished, so its use is a little bit complicated.
You will need the below files ("Example.bat", "Process.bat", "csc.bat", "Blink.bat").
The file "Blink.exe" will be created.

Result (actually tested only on Win XP 32 bit):

Code: Select all

Z:\>Example.bat
Blinking sample text.
Start Process.bat
Test input: a
End Process.bat

Z:\>


Example batch file ("Example.bat"):

Code: Select all

@if not exist "Blink.exe" @>nul call csc Blink
@(
   @(
      >nul ping -n 1 127.0.0.2 -w 250
      >&2 call Process.bat
      @echo Y
   ) | @Blink.exe "Blinking sample text." 250
)

Process batch file ("Process.bat"):

Code: Select all

@echo off
echo(Start Process.bat
set /P "=Test input: "

echo(End Process.bat


C# source file ("Blink.cs"):

Code: Select all

using System;
using System.Threading;

class Blink {
   static int Main (string [] args) {
      string text = "Blinking Text.";
      int update = 500;
      if (args.Length > 0) text = args [0];
      if (args.Length > 1) try { update = int.Parse (args[1]); } catch (Exception) { update = 500; }

           BlinkingText statusChecker = new BlinkingText (text);
           Timer stateTimer = new Timer (statusChecker.UpdateText, null, 0, update);
      int returnValue = -1;

      do {
         try {
            switch (Char.ToUpper (Convert.ToChar (Console.Read ()))) {
               case 'Y':
                  returnValue = 1;
                  break;
               case 'N':
                  returnValue = 2;
                  break;
            }

         } catch (OverflowException) {
            returnValue = 3;
         }

      } while (returnValue == -1);

           stateTimer.Dispose();

      return returnValue;
   }
}


class BlinkingText {
   private int x;
   private int y;

   private string [] messages;
   private int index;
   
   public BlinkingText (string message) {
      x = Console.CursorLeft;
      y = Console.CursorTop;
      string [] stringArray = { message, "".PadRight (message.Length) };

      ConsoleColor color = Console.ForegroundColor;
      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine ("{0}", message);
      Console.ForegroundColor = color;

      messages = stringArray;
      index = 1;
   }

   public void UpdateText (Object stateInfo) {
      int actualX = Console.CursorLeft;
      int actualY = Console.CursorTop;
      ConsoleColor color = Console.ForegroundColor;

      Console.CursorLeft = x;
      Console.CursorTop = y;
      Console.ForegroundColor = ConsoleColor.Red;

      Console.WriteLine ("{0}", (messages [index]).ToString ());

      Console.CursorLeft = actualX;
      Console.CursorTop = actualY;
      Console.ForegroundColor = color;

      index = 1 - index;
   }
}

Compile the source file using this Batch ("csc.bat"):

Code: Select all

// // >nul 2> nul & @goto :main
/*
:main
   @echo off
   setlocal
   cls

   set "csc="

   pushd "%SystemRoot%\Microsoft.NET\Framework"
   for /f "tokens=* delims=" %%i in ('dir /b /o:n "v*"') do (
      dir /a-d /b "%%~fi\csc.exe" >nul 2>&1 && set "csc="%%~fi\csc.exe""
   )
   popd

   if defined csc (
      echo most recent C#.NET compiler located in:
      echo %csc%.
   ) else (
      echo C#.NET compiler not found.
      goto :eof
   )

   %csc% /nologo /optimize /warnaserror /nowin32manifest /debug- /target:exe /out:"%~f1.exe" "%~f1.cs"
rem   %csc% /?
   goto :eof
*/

penpen

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Escape Characters

#11 Post by Aacini » 08 Nov 2014 10:29

@penpen:

Interesting idea! You simulate the blink via manually writting and erasing the message with a certain delay. 8) Many years ago, in the old MS-DOS days, I used a similar method to simulate a blinking cursor in graphics mode via showing/erasing an underscore.

This method may be used in AnsiSys.exe program, so all text with the blinking attribute be displayed this way; the only problem is if the screen scroll a line :(

Antonio

penpen
Expert
Posts: 1995
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Escape Characters

#12 Post by penpen » 08 Nov 2014 11:36

Because i don't lock to the console's monitor object there are multiple side effects with windows events such as scrolling (resizing, ...).
But i don't know how to access this monitor object: This is the main reason, why this program is still unfinished.

penpen

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Escape Characters

#13 Post by Yury » 08 Nov 2014 17:05

Squashman wrote:@Yury,
I guess that is one way to pseudo do it. But that is more of a flicker then a blink because of the CLS command.




Without the CLS command:


Code: Select all

@echo off

set "line=Warning! You are about to delete files..."

echo Hello, %username%!
echo.

cd /d "%~dp0"
for /f %%i in ('
 forfiles /m "%~nx0" /c "cmd /c echo 0x08"
') do set BS=%%i
set /p="%line%_:"<nul>"%line%_"
for %%i in ("%line%_") do (
 for /l %%j in (1 1 %%~zi) do (
  call set x=%%x%%%BS%
  call set "y=%%y%% "
  )
 )
set /p="%BS%%BS%  "<nul>"%line%_"
set /p="The flicker: "<nul
for /l %%i in (1 1 100) do (
 findstr /a:ac "^" "%line%_*"
 for /l %%j in (1 1 100) do pause<nul>nul
 set /p=%x%%y%<nul
 for /l %%j in (1 1 100) do pause<nul>nul
 set /p=%x%<nul
 )
del "%line%_"

echo.
echo.
echo Bye, %username%!
timeout /t 2 1>nul

exit /b



.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Escape Characters

#14 Post by Aacini » 09 Nov 2014 00:03

Yury wrote:Without the CLS command:

. . .

@Yury, I like your method! :D I slightly modified it:

Code: Select all

@echo off

set "line=Warning! You are about to delete files..."

echo Hello, %username%!
echo.

cd /d "%~dp0"
for /f %%i in ('
 forfiles /m "%~nx0" /c "cmd /c echo 0x08"
') do set BS=%%i
set /p="%line%_:"<nul>"%line%_"
for %%i in ("%line%_") do (
 for /l %%j in (1 1 %%~zi) do (
  call set x=%%x%%%BS%
  call set "y=%%y%% "
  )
 )
set /p="%BS%%BS%  "<nul>"%line%_"
set /p="The blink: "<nul
for /l %%i in (1 1 6) do (
 findstr /a:ac "^" "%line%_*"
 ping -w 100 -n 2 localhost >NUL
 set /p="%x%" <nul
 findstr /a:ca "^" "%line%_*"
 ping -w 100 -n 2 localhost >NUL
 set /p="%x%" <nul
 )
del "%line%_"

echo.
echo.
echo Bye, %username%!
timeout /t 2 1>nul

exit /b


Antonio

Post Reply