将批输出对齐到列中

时间:2013-07-17 03:35:15

标签: text batch-file cmd alignment

我有一个批处理文件,它接受可变长度的变量。例如输出是这样的:

%name% - %size% - %percentage% - %percentMIN% - %percentMAX%
%name% - %size% - %percentage% - %percentMIN% - %percentMAX%
%name% - %size% - %percentage% - %percentMIN% - %percentMAX%
%name% - %size% - %percentage% - %percentMIN% - %percentMAX%
%name% - %size% - %percentage% - %percentMIN% - %percentMAX%

因为前两列长度不同,所以输出会查看所有位置。批处理文件中是否有任何方法使列对齐?我做了一些研究,看起来有一种方法可以通过用空格填充一堆字符来实现它吗?但这似乎不起作用?还有其他办法吗?

2 个答案:

答案 0 :(得分:4)

@ECHO OFF
SETLOCAL
SET "spaces=                               "
SET "somethingelse=Some other data"
SET "name=abc"
SET /a size=123
CALL :formatout
SET "name=abcdefghijkl"
SET /a size=12345678
CALL :formatout
SET "name=a"
SET /a size=3
CALL :formatout

GOTO :EOF

:formatout
CALL :padright name 18
CALL :padleft size 13
ECHO +%name%+%size%+%somethingelse%
GOTO :eof

:padright
CALL SET padded=%%%1%%%spaces%
CALL SET %1=%%padded:~0,%2%%
GOTO :eof

:padleft
CALL SET padded=%spaces%%%%1%%
CALL SET %1=%%padded:~-%2%%
GOTO :eof

这个演示应该让你走在正确的轨道上......

输出

+abc               +          123+Some other data
+abcdefghijkl      +     12345678+Some other data
+a                 +            3+Some other data

答案 1 :(得分:0)

 set test=A test
        set spce=                 .   
        set spce2=%spce:~0,16%
        set ntest=%test:~0,16%%spce2%
        set ntest2=%ntest:~0,16%

ECHO 123456789012345678  >> c:\temp\testfile.txt
echo %ntest2%^|other field>> c:\temp\testfile.txt