How to move cursor upwards without cls?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

How to move cursor upwards without cls?

#1 Post by PaperTronics » 10 Oct 2017 06:23

Hey guys!,

So while I was experimenting with graphics for my new pure batch app, I came across a problem. I wanted to move the cursor back to 0,0 from any position on the cmd console, without clearing the screen. As I do with most of my problems, I searched the solution on the internet. I found jeb's attempt at the same problem but as he was using JS, I couldn't use the script.

I haven't developed any code for this problem as I was just brainstorming how I would code it, and realized it was impossible to do it without clearing the screen. If anyone can help me with my problem, it would be greatly appreciated.


Thanks,
PaperTronics

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

Re: How to move cursor upwards without cls?

#2 Post by Aacini » 10 Oct 2017 06:30


PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to move cursor upwards without cls?

#3 Post by PaperTronics » 11 Oct 2017 06:11

@Aacini - I tried both of the links, but I couldn't understand the usage. Hence I wasn't able to move my cursor to the desired location. I know it's too much to ask, but can you develop a piece of code for me that moves the cursor to 0,0 again?



Thanks for the help,
PaperTronics

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How to move cursor upwards without cls?

#4 Post by einstein1969 » 11 Oct 2017 08:28

The timeout command go at 0,1 (x,y) and not 0,0 and delete the INITIAL next line partially(Line 4). IT leave a message that I delete with the CMD part with set /P.

example:

Code: Select all

@echo off
echo.      Line 0
Echo.      Line 1
echo.      Line 2
Echo.      Line 3
call :goto_home
echo.Home
pause>nul
exit /B

:goto_home
 timeout /t 1 > con | cmd /Q /C"for /F "usebackq delims= " %%C in (`copy /z "%~f0" nul`) do set/p".=.%%C                                                                  "
exit /B


EDIT: But you can use the other method for go from 0,1 to 0,0. ....

Einstein1969

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

Re: How to move cursor upwards without cls?

#5 Post by Aacini » 11 Oct 2017 22:06

This code show "Home" at screen home, position (0,0). However, the method destroy the first 16 characters in second line (line=1). If such characters are known, you may recover they via an ECHO command placed in place of PAUSE command.

Code: Select all

@echo off
for /L %%i in (1,1,6) do echo Line %%i
call :MoveCursorHome
echo Home
pause
goto :EOF

:MoveCursorHome
setlocal
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"
timeout /T 1 > CON | cmd /Q /C for /F %%C in ('copy /Z "%~F0" NUL') do set /P "=.%%C%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%" ^& set /P "=.%TAB%%TAB%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS%%BS% %BS%"
exit /B

Tested in Windows 8.1

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to move cursor upwards without cls?

#6 Post by PaperTronics » 12 Oct 2017 06:45

@Aacini - Thanks! The script works flawlessly, but, it displays a 0 at 0,0 which I certainly don't want. Other than that, I've noticed no bugs in the script and the great part is that it doesn't even slow down the program execution.



Thanks again,
PaperTronics

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

Re: How to move cursor upwards without cls?

#7 Post by Aacini » 12 Oct 2017 08:43

As a matter of fact, I don't understand why the code correctly show "Home" (with no "0" at 0,0) in my computer because the logic suggests that an additional %BS% is needed at end (after the space), but I tested this code in two computers with Windows 8.1 and it works. Please, do this mod (the long line should end in %BS% %BS%%BS%", two BS's after the space) and report the result and your Windows version...

Antonio

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to move cursor upwards without cls?

#8 Post by jeb » 13 Oct 2017 00:41

Hi,

I tried Aacini's code and it works (Win7 x64), but only when the screen width is set to 80.

When you modify the last SET then it works independent of the screen width.

Code: Select all

 set ... ^& set /P "=.%TAB%%BS%%BS%%%C"

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to move cursor upwards without cls?

#9 Post by PaperTronics » 13 Oct 2017 04:05

@Aacini - Windows 7 34 bit
I've made the mod that you suggested and still it shows a 0 at the top. There's also a line appears at the current cursor position which looks something like this :

Code: Select all

Waiting for 0.1, 


It comes and disappears quickly so I can't c/p the line. I think it is of the timeout command and even the 0 at the top belongs to the timeout command. I've removed your "echo Home" line and because there is no character at the top the 0 appears.

I think I've helped in solving the problem a bit.

@jeb - I made the change you told and the result was this:

Code: Select all

Environment variable not defined


Am I doing something wrong or is it the code itself? Here's how I edited the last SET command:

Code: Select all

timeout /T 1 > CON | cmd /Q /C for /F %%C in ('copy /Z "%~F0" NUL') do set /P "=.%%C%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%" ^&  set ... ^& set /P "=.%TAB%%BS%%BS%%%C"



Thanks,
PaperTronics

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to move cursor upwards without cls?

#10 Post by aGerman » 13 Oct 2017 05:28

Usually "..." is a placeholder for content that didn't change.

Code: Select all

timeout /T 1 > CON | cmd /Q /C for /F %%C in ('copy /Z "%~F0" NUL') do set /P "=.%%C%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%" ^& set /P "=.%TAB%%BS%%BS%%%C"

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to move cursor upwards without cls?

#11 Post by PaperTronics » 13 Oct 2017 06:15

@aGerman - It still doesn't solve my problem. Here's the whole first line of output:

Code: Select all

0              |                                                                                                 0

Only the "|" character is outputted by me. Everything else is the console's magic :mrgreen: .

And here is the code I'm using:

Code: Select all

Call :MoveCursorUp

for /l %%A IN (1,1,5) DO (
   Call :XY 1 5
   Call :colorPrint 0b "!spc! !Folder%%A!"
   Call :colorPrint 09 " |"
   )



Contents of :MoveCursorUp and :XY:

Code: Select all

:MoveCursorUp
setlocal
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"
timeout /T 1 > CON | cmd /Q /C for /F %%C in ('copy /Z "%~F0" NUL') do set /P "=.%%C%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%%TAB%" ^& set /P "=.%TAB%%BS%%BS%%%C"

:XY
set "X=%~1"
set "Y=%~2"
set spc=

for /l %%A IN (1,1,!X!) Do Set "spc=!spc! "
for /l %%A IN (1,1,!Y!) Do Echo.

Goto :eof



So any other suggests on how I could solve the problem?


EDIT: The console width and height is "120,120"
Any Help is greatly appreciated,
PaperTronics

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

Re: How to move cursor upwards without cls?

#12 Post by ShadowThief » 13 Oct 2017 06:49

If you don't need to do it too often, you can call a PowerShell command to move the cursor.

Of course, PowerShell being the laughably verbose language that it is, by "command" I actually mean "five commands."

Code: Select all

set "pos_cmd=$U=$Host.ui.rawui;$CursorPosition=$U.CursorPosition;"
set "pos_cmd=%pos_cmd%$CursorPosition.X=0;$CursorPosition.Y=0;"
set "pos_cmd=%pos_cmd%$U.CursorPosition=$CursorPosition;"
powershell -command "&{%pos_cmd%}"

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

Re: How to move cursor upwards without cls?

#13 Post by Aacini » 13 Oct 2017 08:21

@PaperTronics:

Change the last SET /P command by this one:

Code: Select all

^& set /P "=.%TAB%%BS%%BS%%%C %%C"

Antonio

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to move cursor upwards without cls?

#14 Post by aGerman » 13 Oct 2017 14:26

@ShadowThief
I think the SetCursorPosition method of the System.Console class would have been easier.

Code: Select all

@echo off &setlocal
echo 0123456789
for /l %%i in (1 1 9) do echo %%i

for %%i in ("3 2","5 1","0 0","9 9","0 4") do (
  call :gotoxy %%~i
  >nul timeout 2
)
exit /b

:gotoxy column line
powershell -ExecutionPolicy Bypass -Command "[Console]::SetCursorPosition(%~1,%~2)"
exit /b

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to move cursor upwards without cls?

#15 Post by PaperTronics » 13 Oct 2017 21:37

@ShadowThief - I need to call the function like at least 50 times in my program, if that isn't too 'often' for you.

@aGerman - Thanks! Your modified code works fluently, and without any errors. So I'm gonna stick with it. Although I have noticed that its slow if called repeatedly. So it can only be used for moving the cursor upwards.

@Aacini - Your Pure Batch attempt at it still isn't solving anything. But now my problem is solved by aGerman's ShadowThief modified code.



Thanks again to all of you,
PaperTronics

Post Reply