Adding space to line to make it fixed length

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kushal_29
Posts: 1
Joined: 22 Nov 2015 10:05

Adding space to line to make it fixed length

#1 Post by kushal_29 » 22 Nov 2015 10:14

I want to make last column width fixed by adding space. suppose I have data lik e

1 aaa abcdefgh
2 bb abcd
3 ccc abcde

This should be output into a file like -

1 aaa abcdefgh "
2 bb abcd "
3 ccc abcde "

Here I want to make last column data length as 10 characters. I have put " just to denote white space here.

Please help!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Adding space to line to make it fixed length

#2 Post by aGerman » 22 Nov 2015 14:32

You need to have your "last column" in a separate variable.

Code: Select all

@echo off &setlocal

set "var1=abc"
set "var2=z"

echo before:
echo "%var1%"
echo "%var2%"

call :fix10 var1
call :fix10 var2

echo after:
echo "%var1%"
echo "%var2%"

pause
exit /b

:fix10
setlocal EnableDelayedExpansion
set "s=!%~1!          "
endlocal &set "%~1=%s:~,10%"
exit /b

Regards
aGerman

Post Reply