以自动适合行宽的方式使用 'echo'
Using 'echo' in a way that automatically fits the row width
我需要显示具有完整路径的文件列表,作为处理时要查看的内容。我不喜欢有些结果如此之长,以至于它们一直持续到第二行,令人不快。我有办法 trim 文件的其余部分以适合我当前的 window:
if "%echo%"=="%echo:~,160%" (echo %echo%) else (echo %echo:~,160%...)
这将成功 trim 该行,并在 trimmed 行的末尾显示 '...' 以表示文件名被截断,如下所示:
"d:\this is\a long\path to\a 树中某处的文件 (21-08-22) ke..."
此方法的缺点是只能处理特定尺寸 window。我的问题是如何使它自动进行?有没有一种方法可以自动检测每行的字符,以便每个人都以正确的方式切割每一行,每种尺寸 window?
window宽度可以由mode con
决定
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=: " %%a in ('mode con') do if "%%a"=="Columns" set "width=%%b"
set /a width-=4
set "text=123456"
call :showline
set "text=123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
call :showline
exit /b
:showline
if "!text!"=="!text:~,%width%!" (echo !text!) else (echo !text:~,%width%!...)
exit /b
这可以批量完成,但是,您需要在每次输出前检查列数,以防用户调整了控制台的大小。
请注意,对每个输出执行此操作会增加大量开销。
@Echo off
(Set \n=^^^
%= \n newline var =%
)
%= Begin macro COUT definition =% Set COUT=For %%n in (1 2)Do If %%n==2 (%\n%
%= Strip up to 1 leading space =% If "!COUT_input:~0,1!"==" " Set "COUT_input=!COUT_input:~1!"%\n%
%= Capture column value from mode =% For /f "tokens=2 Delims=:" %%G in ('Mode Con^^^|%__APPDIR__%findstr.exe /lic:"Columns:"')Do (%\n%
%= Offset column value by 3 =% For /f %%v in ('Set/A %%G - 3')Do (%\n%
%= Compare input against len. cap =% If "!COUT_input:~,%%v!"=="!COUT_input!" (%\n%
%= Display input if LSS cap =% Echo(!COUT_input:~0,%%v!%\n%
%= Display input... if GTR Cap =% ) Else Echo(!COUT_input:~0,%%v!...%\n%
))%\n%
%= End the macro environment =% Endlocal%\n%
%= Enable macro env + Capt. input =% ) Else Setlocal EnableDelayedExpansion ^& Set COUT_input=
:Demo
mode 30,30
%COUT% %computername%;%TEMP%;%__APPDIR__%
Pause
mode 160,30
%COUT% %computername%;%TEMP%;%__APPDIR__%
TImeout /t 3 /Nobreak
PowerShell 在 $Host 变量中提供控制台宽度。
$TEXT='01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
$TextLength = $TEXT.Length
$ScreenWidth = $Host.UI.RawUI.WindowSize.Width - 3
if ($TextLength -gt $ScreenWidth) {
$OutText = $TEXT.Substring(0, [Math]::min($TEXT.Length, $ScreenWidth)) + '...'
} else {
$OutText = $TEXT
}
Write-Host $OutText
如果您必须 运行 来自 cmd.exe
shell,可以使用以下内容。
SET "TEXT=01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
powershell -NoLogo -NoProfile -Command ^
"$TextLength = '%TEXT%'.Length;" ^
"$ScreenWidth = $Host.UI.RawUI.WindowSize.Width - 3;" ^
"if ($TextLength -gt $ScreenWidth) {" ^
"$OutText = '%TEXT%'.Substring(0, [Math]::min('%TEXT%'.Length, $ScreenWidth)) + '...'" ^
"} else {" ^
"$OutText = '%TEXT%'" ^
"}" ^
"Write-Host $OutText"
我需要显示具有完整路径的文件列表,作为处理时要查看的内容。我不喜欢有些结果如此之长,以至于它们一直持续到第二行,令人不快。我有办法 trim 文件的其余部分以适合我当前的 window:
if "%echo%"=="%echo:~,160%" (echo %echo%) else (echo %echo:~,160%...)
这将成功 trim 该行,并在 trimmed 行的末尾显示 '...' 以表示文件名被截断,如下所示:
"d:\this is\a long\path to\a 树中某处的文件 (21-08-22) ke..."
此方法的缺点是只能处理特定尺寸 window。我的问题是如何使它自动进行?有没有一种方法可以自动检测每行的字符,以便每个人都以正确的方式切割每一行,每种尺寸 window?
window宽度可以由mode con
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=: " %%a in ('mode con') do if "%%a"=="Columns" set "width=%%b"
set /a width-=4
set "text=123456"
call :showline
set "text=123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
call :showline
exit /b
:showline
if "!text!"=="!text:~,%width%!" (echo !text!) else (echo !text:~,%width%!...)
exit /b
这可以批量完成,但是,您需要在每次输出前检查列数,以防用户调整了控制台的大小。
请注意,对每个输出执行此操作会增加大量开销。
@Echo off
(Set \n=^^^
%= \n newline var =%
)
%= Begin macro COUT definition =% Set COUT=For %%n in (1 2)Do If %%n==2 (%\n%
%= Strip up to 1 leading space =% If "!COUT_input:~0,1!"==" " Set "COUT_input=!COUT_input:~1!"%\n%
%= Capture column value from mode =% For /f "tokens=2 Delims=:" %%G in ('Mode Con^^^|%__APPDIR__%findstr.exe /lic:"Columns:"')Do (%\n%
%= Offset column value by 3 =% For /f %%v in ('Set/A %%G - 3')Do (%\n%
%= Compare input against len. cap =% If "!COUT_input:~,%%v!"=="!COUT_input!" (%\n%
%= Display input if LSS cap =% Echo(!COUT_input:~0,%%v!%\n%
%= Display input... if GTR Cap =% ) Else Echo(!COUT_input:~0,%%v!...%\n%
))%\n%
%= End the macro environment =% Endlocal%\n%
%= Enable macro env + Capt. input =% ) Else Setlocal EnableDelayedExpansion ^& Set COUT_input=
:Demo
mode 30,30
%COUT% %computername%;%TEMP%;%__APPDIR__%
Pause
mode 160,30
%COUT% %computername%;%TEMP%;%__APPDIR__%
TImeout /t 3 /Nobreak
PowerShell 在 $Host 变量中提供控制台宽度。
$TEXT='01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
$TextLength = $TEXT.Length
$ScreenWidth = $Host.UI.RawUI.WindowSize.Width - 3
if ($TextLength -gt $ScreenWidth) {
$OutText = $TEXT.Substring(0, [Math]::min($TEXT.Length, $ScreenWidth)) + '...'
} else {
$OutText = $TEXT
}
Write-Host $OutText
如果您必须 运行 来自 cmd.exe
shell,可以使用以下内容。
SET "TEXT=01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
powershell -NoLogo -NoProfile -Command ^
"$TextLength = '%TEXT%'.Length;" ^
"$ScreenWidth = $Host.UI.RawUI.WindowSize.Width - 3;" ^
"if ($TextLength -gt $ScreenWidth) {" ^
"$OutText = '%TEXT%'.Substring(0, [Math]::min('%TEXT%'.Length, $ScreenWidth)) + '...'" ^
"} else {" ^
"$OutText = '%TEXT%'" ^
"}" ^
"Write-Host $OutText"