我正在尋找一種方法來在批處理檔案中使用反向換行(如果我理解正確的話)。預先列出它 - 它必須是批處理兼容的 - 不能使用外部可執行檔案,因為它將運行的安全環境。
我有以下設定換行/回車:
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 命令列/批處理檔案語法有這樣的東西嗎?
uj5u.com熱心網友回復:
就像@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- 恢復上次保存的游標位置
uj5u.com熱心網友回復:
事實上,有一個技巧可以生成等效于任何 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: "
該方法在這篇文章中有詳細描述,這里還有另一個例子。
此方法適用于所有 Windows 版本。為了在 Windows 10 中作業,您必須在 cmd.exe 視窗屬性中啟用使用舊版控制臺。但是,這樣做將禁用移動游標的 VT100 ANSI 轉義序列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344598.html
下一篇:如何計算重復的物件屬性?
