Home > Back-end >  Reverse Line Feed in Batch file
Reverse Line Feed in Batch file

Time:10-29

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?

CodePudding user response:

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

  • Related