批处理文件中的反向换行
Reverse Line Feed in Batch file
我正在寻找一种在批处理文件中使用反向换行(如果我正确理解这个概念的话)的方法。预先列出 - 它必须是批处理兼容的 - 不能使用外部可执行文件,因为安全环境这将是 运行.
我有以下设置 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=^
现在,这可以按预期使用 set /p
提示等产生很酷的效果。我想要完成的是使用此方法的带有“文本框”的提示,但类似以下:
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 *************************************************************************************
现在,我以前做过非常相似的事情来实现类似的目标,但是没有成功地提供一个围绕它的边框的解决方案。
下面给出的示例与上面的几乎相同,但没有边框:
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:|____________________________________________________________________
Windows命令Line/Batch文件语法有这样的东西吗?
就像@Ken White 已经说过的,没有 reverse line feed
,但是您可以使用 VT100(ANSI 转义码)移动光标,至少在 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
- 将光标向上移动两行
ESC[11C
- 将光标向右移动 11 个位置
甚至更好,不计
echo **********************
echo * Address: %ESC%7_________ *
echo **********************
set /p "=%ESC%8" < nul
set /p Address=
ESC7
- 保存当前光标位置
ESC8
- 恢复上次保存的光标位置
确实,有一个技巧可以生成在任何 Windows 版本中都有效的 反向换行 的等价物:
@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: "
该方法在 this post and there is another example 中有详细描述。
此方法适用于所有 Windows 版本。为了在 Windows 10+ 中工作,您必须在 cmd.exe Window 属性中启用 Use Legacy Console。但是,这样做会禁用移动光标的 VT100 ANSI 转义序列。
我正在寻找一种在批处理文件中使用反向换行(如果我正确理解这个概念的话)的方法。预先列出 - 它必须是批处理兼容的 - 不能使用外部可执行文件,因为安全环境这将是 运行.
我有以下设置 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=^
现在,这可以按预期使用 set /p
提示等产生很酷的效果。我想要完成的是使用此方法的带有“文本框”的提示,但类似以下:
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 *************************************************************************************
现在,我以前做过非常相似的事情来实现类似的目标,但是没有成功地提供一个围绕它的边框的解决方案。
下面给出的示例与上面的几乎相同,但没有边框:
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:|____________________________________________________________________
Windows命令Line/Batch文件语法有这样的东西吗?
就像@Ken White 已经说过的,没有 reverse line feed
,但是您可以使用 VT100(ANSI 转义码)移动光标,至少在 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
- 将光标向上移动两行
ESC[11C
- 将光标向右移动 11 个位置
甚至更好,不计
echo **********************
echo * Address: %ESC%7_________ *
echo **********************
set /p "=%ESC%8" < nul
set /p Address=
ESC7
- 保存当前光标位置
ESC8
- 恢复上次保存的光标位置
确实,有一个技巧可以生成在任何 Windows 版本中都有效的 反向换行 的等价物:
@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: "
该方法在 this post and there is another example
此方法适用于所有 Windows 版本。为了在 Windows 10+ 中工作,您必须在 cmd.exe Window 属性中启用 Use Legacy Console。但是,这样做会禁用移动光标的 VT100 ANSI 转义序列。