本帖最后由 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="
复制代码
|