Reverse Line Feed in Batch file

Viewed 161

I am looking to find a way to utilize reverse linefeed (if I understand the concept properly) within a Batch file. Listing this up front - it MUST be batch compatible - cannot use external executables because of the secured environment this will be running.

I have the following to setup a linefeed/carriage return:


REM Produces a Carriage Return effect for use later.
for /f %%a in ('copy "%~f0" nul /z') do set "cr=%%a"
REM Produces a Linefeed effect for later use.  WARNING - must keep 2 lines of code blank after this command!
set lf=^


Now, this works as expected for producing cool effects with set /p prompts, etc. What I'm wanting to accomplish is a prompt with a "text box" using this method, but with something like the following:


set /p "var=*************************************************************************************!lf!!cr!* Enter the Street Address of this Store.  [Use as many characters as you need]     *!lf!!cr!* Address:_____________________________________________________________________     *!lf!!cr!*************************************************************************************!REVERSE_LF!!cr!* Address:"

REM Intended Output:

REM NOTE placed a | symbol to indicate where expected cursor would be when user is typing the Address

REM *************************************************************************************
REM * Enter the Street Address of this Store.  [Use as many characters as you need]     *
REM * Address:|____________________________________________________________________     *
REM *************************************************************************************

Now, I have done something very similar before to achieve a similar goal, but have not been successful in providing a solution with a border around it.

Example given below is almost identical to the above, but does not have the border:


set /p "var=Enter the Street Address of this Store.  [Use as many characters as you need]!lf!!cr!Address:_____________________________________________________________________!cr!Address:"

Rem Output:

Rem Note: as before - using pipe symbol to indicate where cursor is when using this method

Rem Enter the Street Address of this Store.  [Use as many characters as you need]
REM Address:|____________________________________________________________________

Is there such a thing for Windows Command Line/Batch File syntax?

2 Answers

Like @Ken White already said, there is no reverse line feed, but you could use VT100 (ANSI escape codes) to move the cursor, at least on windows10.

@echo off

REM Produce an escape character
for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "ESC=%%a"

echo **********************
echo * Address: _________ *
echo **********************
set /p "=%ESC%[2A%ESC%[11C" < nul
set /p Address=

ESC[2A - Move the cursor two lines up
ESC[11C - Move the cursor 11 positions to right

Or even better, without counting

echo **********************
echo * Address: %ESC%7_________ *
echo **********************
set /p "=%ESC%8" < nul
set /p Address=

ESC7 - Save the current cursor position
ESC8 - Restore the last saved cursor position

Indeed, there is a trick that generates the equivalent of a reverse line feed that works in any Windows version:

@echo off
setlocal EnableDelayedExpansion

rem Generate 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 Assemble the "one line up" control string with the proper number of BSs
for /F "tokens=2" %%a in ('mode con ^| findstr "Col"') do set /A "cntBS=(%%a+7)/8"
set "lineUp=" & for /L %%i in (1,1,%cntBS%) do set "lineUp=!lineUp!!BS!"

echo **********************
echo * Address: _________ *
echo **********************
echo %TAB%!BS!!BS!!lineUp!!lineUp!
set /P "Address=* Address: "

The method is described with detail at this post and there is another example here.

This method works in all Windows versions. In order to work in Windows 10+ you must enable Use Legacy Console in the cmd.exe Window Properties. However, doing that will disable the VT100 ANSI escape sequences that moves the cursor.

Related