如何在 CMD 代码输出中对齐空格

如何在 CMD 代码输出中对齐空格

我的批处理代码:

@echo off
set phones="iPhone 12" "Galaxy S5 4G" "Nokia 7"
set manufs=US "South Korea" Finland
SetLocal EnableDelayedExpansion
set n=& for %%a in (%phones%) do set /a n+=1& set phone[!n!]=%%~a
set n=& for %%a in (%manufs%) do set /a n+=1& set manuf[!n!]=%%~a
for /L %%a in (1 1 %n%) do echo !phone[%%a]! : !manuf[%%a]!
pause 

输出:

iPhone 12 : US
Galaxy S5 4G : South Korea
Nokia 7 : Finland

如何在 : 之前对齐空格/制表符,因此它变成:

iPhone 12    : US
Galaxy S5 4G : South Korea
Nokia 7      : Finland

更新 1<<30/03/2024>>

我发现这个方法并且工作正常。

@echo off
set HPs="iPhone 12" "Galaxy S5 4G" "Nokia 7"
set CTs=US "South Korea" Finland
SetLocal EnableDelayedExpansion
set n=& for %%a in (%HPs%) do set /a n+=1& set HP[!n!]=%%~a
set n=& for %%a in (%CTs%) do set /a n+=1& set CT[!n!]=%%~a
set n=& for %%a in (%HPs%) do (
    set /a n+=1& set $HP=%%~a
    for /L %%i in (0 1 12) do if "!$HP:~%%i,1!" NEQ "" set Len=%%i
    set Len[!n!]=!Len!& set Lens=!Lens! !Len!
)
for %%a in (!Lens!) do (if not defined max Set max=%%a
if %%a GTR !max! Set max=%%a)
set "space= "& for /L %%a in (1 1 !max!) do set Spaces=!Spaces!!space!

rem Finally
CLS& for /L %%V in (1 1 %n%
) do call echo !HP[%%V]! %%Spaces:~!Len[%%V]!%%: !CT[%%V]!
pause

输出:

iPhone 12    : US
Galaxy S5 4G : South Korea
Nokia 7      : Finland

也许有人可以编写更简单、更快的代码

相关内容