0

I'm trying to make a little 'game' through batch, and need to get an input from the user and use that, but I need to get a variable (through set /p or something like it) and then clear the line/hide the output (as it usually just outputs whatever you entered). I can't use CLS however, as the other lines need to stay there. I do, however, want to be able to see what you're typing.

I tried many things I found on here that clear lines, like

@echo off
setlocal enableDelayedExpansion

:: Define CR to contain a carriage return (0x0D)
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"

<nul set/p"=test!CR!"
pause >nul

pause

but that only does something like echo, I can't use it to get a variable.

I need something like this:

@echo off
set /p variable1=

 [clear line]

echo %variable1%
pause
  • Hi there! Can you please post what you actually tried so far? May be one of your approaches can inspire some answers here. – dashdashzako Jun 06 '19 at 12:10
  • Add your attempts to your question by [edit]ing it, don't post them as comments! And consult [mcve]! I don't know how to move the cursor up one line, which is what you need here. Maybe a stupid idea, but: what about [`cls`](https://ss64.com/nt/cls.html)? – aschipfl Jun 06 '19 at 12:20
  • I hope this is good, sorry for confusion, first time posting here. As I mentioned, I can't use CLS as I need lines that were printed before to stay. – ThosePixels Jun 06 '19 at 12:22
  • there are a lot of duplicates: [How do I clear ONLY ONE LINE of cmd?](https://stackoverflow.com/q/42450295/995714), [CLS (clear) a single line?](https://stackoverflow.com/q/11972249/995714), [How to clear selected lines in Batch instead of the whole screen?](https://stackoverflow.com/q/44126279/995714), [How to overwrite the same line in command output from batch file](https://superuser.com/q/82929/241386), [Overwrite line in Windows batch file?](https://superuser.com/q/1166580/241386) – phuclv Jun 07 '19 at 16:44
  • Possible duplicate of [How to clear selected lines in Batch instead of the whole screen?](https://stackoverflow.com/questions/44126279/how-to-clear-selected-lines-in-batch-instead-of-the-whole-screen) – phuclv Jun 07 '19 at 16:45

1 Answers1

0
@echo off
setlocal EnableDelayedExpansion

rem Get a CR, BS and TAB characters
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"

rem Initialize spaces; used to delete previous line
set "spaces=                                                         "

rem Get the input line
set /P "variable1=Enter input: "

rem Move cursor one line up: TAB+BS+BS
rem Move cursor to begin of line: CR
rem Delete previous line: spaces
rem Leave cursor at beginning of the line: CR
set /P "=.%TAB%!BS!!BS!!CR!%spaces%!CR!" < NUL

echo %variable1%

You may read a detailed explanation on the method used at Move cursor to any position using just ECHO command

IMPORTANT! The "bug" that makes this method possible was fixed in Windows 10, so you need to enable "Legacy console mode" in this case. Of course, you may also move the cursor using ANSI escape sequences in Windows 10. However, the solution presented here works in all Windows versions...

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks a lot! It works good for the most part, is there anything to do about the dot it prints when i don't immedeatly echo it? – ThosePixels Jun 06 '19 at 13:06
  • Change `set /P "=.%TAB%!BS!!BS!!CR!%spaces%!CR!" < NUL` by `set /P "=.!BS!%TAB%!BS!!BS!!CR!%spaces%!CR!" < NUL` – Aacini Jun 06 '19 at 13:13