Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning batch output into columns

I have a batch file that takes variables of variable length. For example output is like this:

%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%

Because the first 2 columns vary in length the output looks over all the place. Is there any way in a batch file to have the columns align? I've done some researching and it seems like there is a way to do it by padding the beginning bunch of characters with spaces? But that doesn't seem to work? Is there maybe another way?

like image 483
Duxa Avatar asked Jul 17 '13 03:07

Duxa


1 Answers

@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

This demo should set you on the right track...

output is

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

How it works (in response to comment)

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

Let's start with the :formatout routine. It calls :pad???` for each of the required padding operations, then strings the results together for form an output line.

:padright requires two parameters. The first is the name of the variable whose contents are to be padded and the second the length to which they are to be padded.

CALL SET padded=%%%1%%%spaces%

uses a parsing trick. The call executes the set in a cmd subshell. The instruction executed in that subshell is

SET padded=%%.%1.%%.%spaces%

where I've used . to separate the syntactic elements.

%% here is an escaped-% as % is its own escape character (other awkward characters like &,>,<, ) etc. are escaped by ^ eg. ^&. You need to escape a character if you want that character to be interpreted literally instead of having its special meaning within batch syntax. Hence, if you want to echo a > character, you need to escape the > (ie ^>) to tell batch that it is the literal that is required, not a redirection operation.

%1 is replaced by the first parameter supplied to the routine, name in the example.

So what is actually executed is

set padded=%name%%spaces%

stringing a goodly number of spaces onto the end of the current value of the variable name (Note that this post is so old that it predates the use of the set "var=value" syntax I now use)

consequently, padded now contains the value of name padded with a number of spaces.

CALL SET %1=%%padded:~0,%2%%

Second verse - same as the first - well, almost. The set command executed is SET %1.=.%%.padded:~0,.%2.%%

or

SET name=%padded:~0,18%

since the second parameter provided to :padright is 18.

and this command assigns the first 18 characters of padded to name.

:padleft works the same way, but adding the the spaces before the value of the variable, then selecting the last (second parameter value) characters.

like image 105
Magoo Avatar answered Nov 06 '22 11:11

Magoo