VT100 escape sequences not working in Windows 10?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ludwig
Posts: 5
Joined: 25 Aug 2019 10:33

VT100 escape sequences not working in Windows 10?

#1 Post by ludwig » 25 Aug 2019 11:02

I have an issue where I'm processing a bunch of files in a directory with the command line program ffprobe (part of ffmpeg). The directories will have video clips and ffprobe is run on each to see if certain conditions are met and then carry out ffmpeg modifications depending on those conditions. I've been using a batch file for several years to accomplish this, but I've always wanted to spruce it up a bit so I could share it with others.

Part of what I want to do is go through the list of files in a directory and show those file names on one line (not consecutive lines flowing down the screen) to show the user that they are being processed. I also don't mean file names written one after the other across the screen. I mean, for example, everything stays on the top line, starts writing from the far left and never moves down the screen. Each file name occupies the spot where the previous file name was output. I started out using the following to do that:

Code: Select all

for %%a in ("*.*") do set filename=%%a

for /f %%h in ('copy "%~f0" nul /z') do set "CR=%%h"
set /p ".=!filename! !CR!" < nul
The problem with this is that the file names on the screen aren't deleted when the next file name is written to the screen, it is actually written over the character space that the previous file name occupied and if the first file name has more characters than the next then those "extra" characters remain on the screen when the next file name outputs. An example would be if a long file name, let's say longfilename.txt appears first on the screen and the next file is short.txt then what you see on the screen is:

short.txtame.txt

since the 9 charters in the file name short.txt overwrite only the first 9 characters of the file name longfilename.txt. I don't know if this is because of changes made to batch file behavior in Windows 10, but I suspect this to be the case.

I posted this question on reddit and someone suggested the following code using VT100 escape sequences:

Code: Select all

ECHO OFF
FOR /F %%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A”

FOR %%E in (“longfilename.txt” “short.txt” “mediumfile.txt”) DO (
    ECHO %ESC%[1G%ESC%[2K%%~E
    PAUSE>NUL
)
but the output of that on my cmd screen is:

1G2KΓÇ£longfilename.txtΓÇ¥
1G2KΓÇ£short.txtΓÇ¥
1G2KΓÇ£mediumfile.txtΓÇ¥

I suspect there's something I need to have set in order for these codes to work, possibly in the registry, that isn't actually set. I've Googled this issue and found a note on Stack Overflow that says that I can test if VT100 sequences are enabled by issuing this to Powershell: "?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);

That line does output properly assigning colors to: "ele ct roni X tar" as intended, however I was worried that making changes to the registry that they suggested only applied to Powershell and not the cmd window.

But, as I describe above this stuff isn't working in the plain old cmd window. I am not an expert in batch writing and manage to get most things to work only by Googling them and finding where someone else has tried to do what I want to do so PLEASE don't suggest I try to do this in a Powershell script or C. I know a few languages, but those two aren't part of my repertoire. This is just a pet project of mine.

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: VT100 escape sequences not working in Windows 10?

#2 Post by dbenham » 25 Aug 2019 11:30

If you are OK with displaying the file names on the top line, then there is no need for VT100 sequences.

Simply do CLS & ECHO file.

You can easily make your original code work on any line with a small change:

Code: Select all

set /p "=!CR!                                                                                  !CR!!filename!" < nul
Just make sure the number of spaces between the to carriage returns is greater than your longest file name. But not so many spaces that it wraps to the next line.



The escape sequences only work if the "Use legacy console" option is unchecked in the options tab of the console settings dialog.

The escape sequences you are attempting to use will not work if your are using ECHO. You would also need to move up one line.

The sequences are documented at https://docs.microsoft.com/en-us/window ... -sequences.

ludwig
Posts: 5
Joined: 25 Aug 2019 10:33

Re: VT100 escape sequences not working in Windows 10?

#3 Post by ludwig » 25 Aug 2019 11:43

dbenham wrote:
25 Aug 2019 11:30
If you are OK with displaying the file names on the top line, then there is no need for VT100 sequences.

Simply do CLS & ECHO file.
I just said on the first line for the sake of simplicity. There are other things on the screen that need to be there so I can't use CLS.

ludwig
Posts: 5
Joined: 25 Aug 2019 10:33

Re: VT100 escape sequences not working in Windows 10?

#4 Post by ludwig » 25 Aug 2019 11:47

dbenham wrote:
25 Aug 2019 11:30
If you are OK with displaying the file names on the top line, then there is no need for VT100 sequences.

Simply do CLS & ECHO file.

You can easily make your original code work on any line with a small change:

Code: Select all

set /p "=!CR!                                                                                  !CR!!filename!" < nul
Just make sure the number of spaces between the to carriage returns is greater than your longest file name. But not so many spaces that it wraps to the next line.



The escape sequences only work if the "Use legacy console" option is unchecked in the options tab of the console settings dialog.

The escape sequences you are attempting to use will not work if your are using ECHO. You would also need to move up one line.

The sequences are documented at https://docs.microsoft.com/en-us/window ... -sequences.
Sorry I didn't see all of your post when I responded just now. I use something like what you suggested already, but it seems like a kludgey way to do it

Code: Select all

call :displayexe filename
for /f %%h in ('copy "%~f0" nul /z') do set "CR=%%h"
set /p ".=Checking..... !shortfilename!!periods!!CR!" <nul
set /p ".=                                                                              !CR!" < nul
The little box is ASCII for backspace. I couldn't get it it to work otherwise.

If you can't use escape sequences in ECHO then how do you use them?

I've already checked to see if use legacy console is checked and it is not.

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

Re: VT100 escape sequences not working in Windows 10?

#5 Post by penpen » 25 Aug 2019 14:40

If you are using echo, then you end up in the next line, so you have to go 1 line up (using "%ESC%A")-
The other escape codes are:
- Go to the column 1 (using "%ESC%[1G"),
- delete the entire line content (using "!ESC![2K").

And watch out those crazy doublequotes, that you are using:
They add 'ô' and 'ö' to all filenames and add an additional character to the escape sequence.

(Finally you should add an '@' character in front of "echo off".)

So that should work (if legacy mode is off and if your browser/editor doesn't mess up the doublequotes again):

Code: Select all

@ECHO OFF
setlocal enableExtensions disableDelayedExpansion
FOR /F %%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"

:: that line should be in white text color
echo %ESC%[38;5;195mHello World%ESC%[0m
echo(

FOR %%E in ("longfilename.txt" "short.txt" "mediumfile.txt") DO (
	ECHO %ESC%A%ESC%[K%ESC%[1G%ESC%[2K%%~E
	>nul pause
)

goto :eof
penpen

ludwig
Posts: 5
Joined: 25 Aug 2019 10:33

Re: VT100 escape sequences not working in Windows 10?

#6 Post by ludwig » 25 Aug 2019 15:40

penpen wrote:
25 Aug 2019 14:40

(Finally you should add an '@' character in front of "echo off".)
Yes, I accidentally left that off when I cut and paste.
penpen wrote:
25 Aug 2019 14:40

Code: Select all

:: that line should be in white text color
echo %ESC%[38;5;195mHello World%ESC%[0m
echo(
Why is that echo( line in there? I've not seen that before. I see where "Hello World" fails to output without it.
Last edited by ludwig on 25 Aug 2019 18:40, edited 1 time in total.

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

Re: VT100 escape sequences not working in Windows 10?

#7 Post by penpen » 25 Aug 2019 18:23

ludwig wrote:
25 Aug 2019 15:40
Why is that echo( line in there? I've not seen that before. I see where "Hello World" fails to output without it.
The Hello world doesn't fail to output:
It is just overwritten immediately.

The "echo("-line just echoes an empty line and is needed for the for loop, because the echo command in there moves the cursor one line up.

I think it was that thread explains the echoing newline best:
viewtopic.php?f=3&t=774

ludwig wrote:
25 Aug 2019 15:40
How do I change your example so that the for loop goes through all 3 file names and then stops without closing the window? I don't want to have to press a key to go to the next file name. I know it will cycle through the file names faster than I can see, but that's ok.
You might use a the pause command after the loop finidhed:

Code: Select all

@ECHO OFF
setlocal enableExtensions disableDelayedExpansion
FOR /F %%A in ('ECHO prompt $E^| cmd') DO SET "ESC=%%A"

echo(
FOR %%E in ("longfilename.txt" "short.txt" "mediumfile.txt") DO (
	ECHO %ESC%A%ESC%[K%ESC%[1G%ESC%[2K%%~E
)
>nul pause
goto :eof

penpen

ludwig
Posts: 5
Joined: 25 Aug 2019 10:33

Re: VT100 escape sequences not working in Windows 10?

#8 Post by ludwig » 25 Aug 2019 18:44

penpen wrote:
25 Aug 2019 18:23

You might use a the pause command after the loop finidhed:
Thanks, I just figured it out for myself and came back to edit my post to remove that question. Here is what works for me:

Code: Select all

FOR %%E in ("longfilename.txt" "short.txt" "mediumfile.txt") DO (
	set /p="%ESC%[1G%ESC%[2K%%~E"<nul
)
Using set I don't need the %ESC%A%ESC%[K part.

Thanks so much for your help!

Post Reply