Win10: Move cursor position to 0,0

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
WiVi71
Posts: 19
Joined: 06 Jan 2020 02:33

Win10: Move cursor position to 0,0

#1 Post by WiVi71 » 21 Mar 2020 16:28

I found that "FOR /F %A IN ('"TIMEOUT 1 > CON"') DO REM" command shows a different output than "TIMEOUT 1 > CON" command(In Windows 10).

FOR_TIMEOUT.PNG
FOR_TIMEOUT.PNG (7.2 KiB) Viewed 8456 times
The command moved the cursor position to 4,0
Also, several special characters were displayed on the screen.

So, this code moves cursor position to 0,0.

Code: Select all

:: I don't know if this code will work in other environment.
@ECHO OFF
FOR /F %%A IN ('"ECHO PROMPT $H | CMD"') DO SET "BS=%%A"
FOR /F %%A IN ('"TIMEOUT 1 > CON"') DO REM
<NUL SET /P "PRINT=%BS%%BS%%BS%%BS%TESTED IN WIN 10"
PAUSE > NUL
Win10CP.png
Win10CP.png (34.11 KiB) Viewed 8456 times
Tested in Win10 CMD / Windows Terminal beta

ASCII.png
ASCII.png (6.25 KiB) Viewed 8456 times
Also, according to this table, that output was:

Code: Select all

[BS]0[TAB][CR]/F %A IN ('"TIMEOUT 1 > CON"') DO REM
[TAB][CR]Waiting for 1 seconds, press a key to continue ...
But I'm not sure why ASCII TAB,CR was printed. :?:



EDIT: "CMD /C TIMEOUT 1 > CON" seems to work like "FOR /F %A IN ('"TIMEOUT 1 > CON"') DO REM". Tests on CMD.
Last edited by WiVi71 on 21 Mar 2020 19:11, edited 4 times in total.

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

Re: Win10: Move cursor position to 0,0

#2 Post by penpen » 21 Mar 2020 17:55

Antonio did something like that before using Windows 8.1:
viewtopic.php?f=3&t=6760

WiVi71
Posts: 19
Joined: 06 Jan 2020 02:33

Re: Win10: Move cursor position to 0,0

#3 Post by WiVi71 » 21 Mar 2020 18:41

penpen wrote:
21 Mar 2020 17:55
Antonio did something like that before using Windows 8.1:
viewtopic.php?f=3&t=6760
Outdated.PNG
Outdated.PNG (9.17 KiB) Viewed 8425 times
The codes we used before(timeout 1 > con) moved cursor position to 1,0
So you should have used a script to lift the cursor position up one line to move the cursor to 0,0

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Get a BS and TAB control characters
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"

rem Leave some empty lines and do a PAUSE
echo Three empty lines below + pause
echo/
echo/
echo/
pause

rem Get width of screen buffer, set number of lines to go above
for /F "tokens=2" %%a in ('mode con ^| findstr "Col"') do set /A buffWid=%%a, linesAbove=3

rem Assemble the "go above" control string with the proper number of BSs
set "BSs="
set /A "cntBS = 2 + (buffWid + 7) / 8 * linesAbove"
for /L %%i in (1,1,%cntBS%) do set "BSs=!BSs!!BS!"

rem Move cursor up the desired number of lines
echo %TAB%!BSs!

echo Hello,
echo World
echo This line overwrites the one with PAUSE output
https://stackoverflow.com/a/44138141

This code is available as shown in the following link.
viewtopic.php?p=54462#p54462

But this trick only works up to Windows 8.1..
Instead, "FOR /F %A IN ('"TIMEOUT 1 > CON"') DO REM" can move the cursor position to 0,0 without a string-up trick.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Win10: Move cursor position to 0,0

#4 Post by carlos » 21 Mar 2020 20:32

Hello WiVi71

What about move the cursor to position 0,0 using windows 10 escape sequences. I think is more easy and clear code:

Code: Select all

@ECHO OFF

ECHO LINE 1

FOR /F %%E IN (
'"PROMPT $E &FOR %%_ IN (_) DO REM CURSOR 0 BY @CARLOS"'
) DO <NUL SET /P "=%%E[1;1H"

ECHO LINE 2

PAUSE


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

Re: Win10: Move cursor position to 0,0

#5 Post by penpen » 22 Mar 2020 06:31

WiVi71 wrote:
21 Mar 2020 18:41
The codes we used before(timeout 1 > con) moved cursor position to 1,0
So you should have used a script to lift the cursor position up one line to move the cursor to 0,0
In his first post in the topic (that i linked above) Antonio used:

Code: Select all

for /F %%a in ('timeout /T 1 ^> CON') do rem
(If i remember right:) Under win 8.1, that just behaved the same as without the for loop. So your above discovery was indeed new
and i didn't want to argue against that; i just wanted to point out, the same code behaved differently ("sth like that" but not "the same")
under win 8.1 (so if someone might be tempted to use that code for both systems, he/she/.../they might want to add a version check).


Sidenotes:
You might want to add the "/nobreak" switch.
Somehow i missed the "i'm not sure why ASCII TAB,CR was printed" part:
The characters are carriage return (CR, \r) and newline (NL, \n), so it's a normal (win) endline:

Code: Select all

>con cmd /cecho \r\n == 
Output:

Code: Select all

\r\n == ♪◙
penpen

Post Reply