How to move cursor upwards without cls?
Moderator: DosItHelp
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
How to move cursor upwards without cls?
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
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
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: How to move cursor upwards without cls?
@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
Thanks for the help,
PaperTronics
-
- Expert
- Posts: 960
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: How to move cursor upwards without cls?
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:
EDIT: But you can use the other method for go from 0,1 to 0,0. ....
Einstein1969
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
Re: How to move cursor upwards without cls?
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.
Tested in Windows 8.1
Antonio
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
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: How to move cursor upwards without cls?
@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
Thanks again,
PaperTronics
Re: How to move cursor upwards without cls?
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
Antonio
Re: How to move cursor upwards without cls?
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.
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"
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: How to move cursor upwards without cls?
@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 :
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:
Am I doing something wrong or is it the code itself? Here's how I edited the last SET command:
Thanks,
PaperTronics
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
Re: How to move cursor upwards without cls?
Usually "..." is a placeholder for content that didn't change.
Steffen
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
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: How to move cursor upwards without cls?
@aGerman - It still doesn't solve my problem. Here's the whole first line of output:
Only the "|" character is outputted by me. Everything else is the console's magic .
And here is the code I'm using:
Contents of :MoveCursorUp and :XY:
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
Code: Select all
0 | 0
Only the "|" character is outputted by me. Everything else is the console's magic .
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
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to move cursor upwards without cls?
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."
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%}"
Re: How to move cursor upwards without cls?
@PaperTronics:
Change the last SET /P command by this one:
Antonio
Change the last SET /P command by this one:
Code: Select all
^& set /P "=.%TAB%%BS%%BS%%%C %%C"
Antonio
Re: How to move cursor upwards without cls?
@ShadowThief
I think the SetCursorPosition method of the System.Console class would have been easier.
Steffen
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
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: How to move cursor upwards without cls?
@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
@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