2

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?

k1dfr0std
  • 379
  • 1
  • 15
  • 2
    This is not possible without an external executable. Windows does not directly support specifying the cursor position in batch files. See https://stackoverflow.com/q/4357113/62576 – Ken White Oct 29 '21 at 02:18
  • I appreciate the sentiment, but I'm not trying to pinpoint mouse coordinates, nor use a mouse to place the cursor. In effect with the 2nd example given, the cursor is placed (correctly I might add - used this multiple times) at the end of `Address:` to overwrite the Underscored line as the user types out the lines. I know in linux and other languages there's a method to suss out a Reverse Line Feed (Windows Environs - limited ability to use other lang's), but I am hoping to assign a Reverse Line Feed to a variable in some manner in order to use its effect within the batch file. – k1dfr0std Oct 29 '21 at 02:26
  • Additionally, I am open to a VBScript example, but I would still need to assign the reverse Line Feed to a variable in use within the Batch file. – k1dfr0std Oct 29 '21 at 02:28
  • 2
    There is no such thing as a *reverse line feed* using native Windows batch file capabilities, which is what I said specifically in my previous comment. You'll have to use an external application. – Ken White Oct 29 '21 at 02:32
  • Dang - my response was mainly due to the content of the provided article which was largely unrelated to the ask. Is there a way to utilize powershell or VBScript to assign a variable from within the batch environment? – k1dfr0std Oct 29 '21 at 02:38

2 Answers2

3

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

jeb
  • 78,592
  • 17
  • 171
  • 225
  • I have also tested this now on Windows Server 2019 - similar architecture - and it works here as well. <3 – k1dfr0std Oct 29 '21 at 06:44
  • Not sure if this will always be around, but here's a fairly quick reference guide (last page) for Escape Sequences: https://www.ecse.rpi.edu/courses/CStudio/Old%20MPS%20Labs/MPS_ANSI_Lab_Ex2.pdf I had never heard of this before. Fascinating stuff. – k1dfr0std Oct 29 '21 at 06:46
  • 4
    @k1dfr0std This ANSI stuff is fairly new (to windows). The rest of the world used this since 1979 – jeb Oct 29 '21 at 06:56
  • 2
    Yeah, like [this?](https://stackoverflow.com/questions/63283831/how-can-i-apply-multiple-colors-to-lines-in-windows-command-prompt/63284549#63284549) – Gerhard Oct 29 '21 at 07:04
  • YEAH!!!! Oh boy. . . this is going to create quite the . . . rabbit trail for myself. I'm seriously going to retrofit SO MANY of our tools we've built for my company using Batch to include this stuff. THIS IS BETTER THAN CHRISTMAS. – k1dfr0std Oct 29 '21 at 07:07
  • So - now I gotta know - how do you get the prompt command w/ the escape sequences to do anything? I've read up on this a little more just in this passing time, but the prompt command itself eludes me - are there any good S.O. articles that explain the Prompt command itself? I would love to know more about how this works – k1dfr0std Oct 29 '21 at 07:08
  • 2
    I don't know a question about this, but it's straightforward `prompt $E[41m$P$G$E[m` – jeb Oct 29 '21 at 07:28
  • OK - so: prompt $E[41m - this is literally then the same as calling the `%ESC%[41m` from the batch solution here. And. . . by using the For loops, we are effectively invoking the prompt command every time we do this. $P$G is the current drive/path and `>` Just discovered $E[m terminates the color change. If you do not do this, it colors everything after the prompt. – k1dfr0std Oct 29 '21 at 07:37
2

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I had seen this example after I posted my question and thought it was a neat approach, however, there are things which I'm not allowed to modify (the requirements for this is one of them), so this would not work for my environment (tested and proven) - I do not doubt its ability to do what's needed though when the environment permits this action! – k1dfr0std Oct 30 '21 at 03:00
  • 1
    Ok, but this means that your environment is Windows 10... This method works as is in any Windows version previous to 10. – Aacini Oct 30 '21 at 03:10
  • Correct - and Windows Server 2019. I suppose it would be beneficial to edit this into my question, however, years ago when I started practicing batch, I was in an environment which still dealt with Windows XP and Windows 7, and I thought it a pipe dream, so I'm very thankful to have both of these answers in this question - provides as full a picture as possible for anyone else who may look for this. – k1dfr0std Oct 30 '21 at 03:13