返回列表 发帖
回复 19# qixiaobin0715


    显示宽度其实是取决于字体,等宽还是比例,全角和半角等,控制台一般是consoles字体,是等宽的,全角输入中英文字符也是相同宽度。

TOP

回复 20# buyiyang

批处理没有格式化输出,要对齐要注意两点:
    1.等宽字体和比例字体。等宽字体每个西文宽度相同,才能对齐;而比例字体会将西文宽度按比例调整以便于观看。中文一般都是等宽的。
    2.全角和半角。半角字符占1个标准字符宽度,西文(包括空格)一般为半角;而全角字符占2个标准字符宽度,中文均为全角。
楼上批处理是根据字节计算宽度的,有findstr算偏移量、for的%%~zi增强扩展算size的,都是根据:
    在ansi(gb2312)编码中一个汉字必须是两个值大于127的字节连在一起,并兼容ASCII。所以一个汉字=2个字节=2个标准字符宽度,一个ASCII字符=1个字节=1个标准字符宽度。
其实可以通过if比较区分每个字符是中文还是英文。
还有通过控制台437代码页将双字节的字符分成两个字符来算字符宽度的方法。
如果是utf-8等其他编码方式则会不同。

TOP

本帖最后由 buyiyang 于 2024-7-20 21:55 编辑

回复 25# ppll2030

这个不限列数,还用了两种方法算字符宽度。
@echo off
chcp 437 >nul
set "Separator=@"
set "inputfile=a.txt"
setlocal enabledelayedexpansion
:loop
set /a column+=1
set /a #C%column%max=mark=0
for /f "tokens=%column%" %%i in (%inputfile%) do (
    call :strlen "%%i"
    if !len! geq !#C%column%max! set /a #C%column%max=len
    set mark=1
    )
if %mark% equ 1 goto :loop
set /a column-=1
chcp 936 >nul
set /a lines=0
(for /l %%i in (1,1,%column%) do call :output %%i)>$.temp
set "Separator="&set arg=" delims=%Separator%" "rem"
set "inputfile=$.temp"&(for /l %%i in (1,1,%lines%) do call :output %%i %arg%)>output_%inputfile%
del /q $ $.temp
pause&exit
:strlen
set "str=_%~1"
set "len=0"
for %%n in (64 32 16 8 4 2 1) do (
    if "!str:~%%n,1!" neq "" (
        set/a "len+=%%n"
        set "str=!str:~%%n!"
        )
)
exit /b
:output
if %1 equ 1 goto :output_2
for /f "tokens=1,%1%~2" %%a in (%inputfile%) do (
    if "%%b"=="" (set "field=-") else set "field=%%b"
    %~3 set/p="!field!"<nul>$&for %%i in ($) do (set len=%%~zi)
    %~3 set /a spaceNum=!#C%1max!-len+2,n+=1
    set "outstr=!outstr!%Separator%!field!"
    %~3 for /l %%i in (1,1,!spaceNum!) do set "outstr=!outstr! "
    )
%~3 if %n% geq %lines% set /a lines=n
%~3 set /a n=0
echo,!outstr!
set "outstr="
exit /b
:output_2
for /f "tokens=%1%~2" %%a in (%inputfile%) do (
    if "%%a"=="" (set "field=-") else set "field=%%a"
    %~3 set/p="!field!"<nul>$&for %%i in ($) do (set len=%%~zi)
    %~3 set /a spaceNum=!#C%1max!-len+2,n+=1
    set "outstr=!outstr!%Separator%!field!"
    %~3 for /l %%i in (1,1,!spaceNum!) do set "outstr=!outstr! "
    )
%~3 if %n% geq %lines% set /a lines=n
%~3 set /a n=0
echo,!outstr!
set "outstr="COPY

TOP

回复 36# newswan


    第35行应该是maxWidth - LenHZ

TOP

回复 35# aloha20200628


    使用这段代码如果字段中有=,等批处理分隔符的话会被错误分割;而且以“全文最大字段长度”为标准,当原文件字段长度差距悬殊时会使结果中字符较少的列视觉间隔过大而不协调。

TOP

有的虽然解决了在cmd屏幕里对齐,  但输出到文本但输出到文本又是另一个不对齐现象发生了
娜美 发表于 2024-7-21 17:30

先看看我20楼和30楼说的字体问题……

TOP

算字节长度其实也可以将文本转换为16进制字串进行计算和处理,而且不用担心任何特殊字符。
@echo off&setlocal enabledelayedexpansion
set "input=a.txt"&set /a m=line=1&set "act=set/p=,&set/a m=1"
for %%i in ("%input%") do fsutil file creATenew "%input%.zero" %%~zi >nul
(for /f "tokens=2" %%a in ('fc /b "%input%" "%input%.zero"^|findstr /irc:"[0-9A-F]*: [0-9A-F][0-9A-F] 00"') do (
    if /i "%%a"=="0A" (echo,&set/a m=1,line+=1,col=0) else if /i "%%a"=="0D" (echo,
        for %%l in (!line!) do for /l %%i in (1,1,!col!) do (
            if !C%%i_%%l! gtr !C%%imax! set/a C%%imax=C%%i_%%l
            set "C%%i_%%l="
            ) ) else if /i "%%a"=="20" (%act%) else if /i "%%a"=="09" (%act%) else (
                    set/p=%%a
                    set /a col+=m,m=0
                    set /a C!col!_!line!+=1
                   )
        ))<nul >"%input%.temp"
(for /f "delims=" %%a in (%input%.temp) do (
    set /a col=0
    for %%i in (%%a) do (
        set /a col+=1&set "space="
        set /a size=C!col!max,len=size*2+2
        for /l %%i in (1,1,!size!) do set "space=!space!20"
        set "outstr=%%i!space!"
        for %%i in (!len!) do echo,!outstr:~0,%%i!
        )   
    set /p =0d0a
)   )<nul >"out_%input%"
del /q "%input%.zero" "%input%.temp"
certutil -decodehex -f "out_%input%" "out_%input%" >nul
pauseCOPY
1

评分人数

TOP

返回列表