我也发一个吧。也是用字节数补齐空格的方式。
就是代码啰嗦了一些。而且局限列数为5列。
不知道还能不能精简,望各位老大指点一二。- @echo off
- setlocal enabledelayedexpansion
- rem 计算字符的字节,cmd下输出是对齐状态,导出txt会变形,需改换等宽字体,cmd默认是Consolas。
- REM 获取文本最大字符串的字节
- for /f "delims=" %%a in (1.txt) do for %%s in (%%a) do (
- set/p="%%s"<nul>$
- for %%b in ($) do if %%~zb gtr !maxZ! set "maxZ=%%~zb"
- )
- for /f "tokens=1-4* delims= " %%a in (1.txt) do (
- rem 获取每列字符串的字节
- set "a=%%a"&set "b=%%b"&set "c=%%c"&set "d=%%d"&set "e=%%e"
- set/p="%%a"<nul>$&for %%i in ($) do (set az=%%~zi)
- set/p="%%b"<nul>$&for %%i in ($) do (set bz=%%~zi)
- set/p="%%c"<nul>$&for %%i in ($) do (set cz=%%~zi)
- set/p="%%d"<nul>$&for %%i in ($) do (set dz=%%~zi)
- set/p="%%e"<nul>$&for %%i in ($) do (set ez=%%~zi)
- rem 获取每列字符串的字节差,并用空格补齐
- set/a Na=!maxZ!-!az!&set/a Nb=!maxZ!-!bz!&set/a Nc=!maxZ!-!cz!&set/a Nd=!maxZ!-!dz!&set/a Ne=!maxZ!-!ez!
- for /l %%i in (1,1,!Na!) do set "a=!a! "
- for /l %%i in (1,1,!Nb!) do set "b=!b! "
- for /l %%i in (1,1,!Nc!) do set "c=!c! "
- for /l %%i in (1,1,!Nd!) do set "d=!d! "
- rem for /l %%i in (1,1,!Ne!) do set "e=!e! "
- echo !a! !b! !c! !d! !e!
- )
- del/q $&pause&exit
复制代码
|