center text with echo

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

center text with echo

#1 Post by pumi » 17 Oct 2013 08:08

Hello,

how can I center a text with echo in a batch?
Can you give me a hint please?

Thank in advance for help and
greetings pumi

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: center text with echo

#2 Post by ShadowThief » 17 Oct 2013 08:27

First, figure out how wide your terminal window is. The default is 80 characters, but yours might be different.
Next, find the length of the string you wish to center. There are some string length calculation functions on this site, I assume you know how to search.
Then subtract the length of the string from the width of the window and divide that number by two, rounding down to the nearest whole number.
Finally, add that many spaces to the start of your string and print it.

npocmaka_
Posts: 513
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: center text with echo

#3 Post by npocmaka_ » 17 Oct 2013 08:51

Code: Select all

@echo off

set "string=blahblahblahblahblbllahblahblahblahblahblbllah"
call  :strlen0.3 string len
for /f "tokens=1,2 delims=: " %%a in ('mode^| find "Columns"') do @set cols=%%b
set /a start_from=cols/2-len/2
setlocal enableDelayedExpansion

for /l %%S in (1,1,!start_from!) do (
   set "string= !string!"
)
endlocal & set string=%string%

echo %string%

goto :eof
:strlen0.3  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%A in (2187 729 243 81 27 9 3 1) do (
   set /A mod=2*%%A
   for %%Z in (!mod!) do (
      if "!s:~%%Z,1!" neq "" (
         set /a "len+=%%Z"
         set "s=!s:~%%Z!"
         
      ) else (
         if "!s:~%%A,1!" neq "" (
            set /a "len+=%%A"
            set "s=!s:~%%A!"
         )
      )
   )
  )
  endlocal & if "%~2" neq "" (set %~2=%len%) else echo **%len%**
exit /b

einstein1969
Expert
Posts: 947
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: center text with echo

#4 Post by einstein1969 » 17 Oct 2013 09:44

or more simple, need more test:

Code: Select all

@echo off & setlocal enableDelayedExpansion

set /a size=80-1 & rem screen size minus one

set s=b
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=bla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!

set s=blablablablablabla
for /L %%# in (1,2,!size!) do if "!s:~%size%,1!" == "" set "s= !s! "
set s=!s:~1,%size%!& echo(!s!


result:

Code: Select all

                                        b
                                       bla
                                    blablabla
                                  blablablabla
                                 blablablablabla
                               blablablablablabla


EDIT: Correct for better centering and minor iteration (div 2)

Einstein1969
Last edited by einstein1969 on 18 Oct 2013 04:46, edited 3 times in total.

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: center text with echo

#5 Post by pumi » 18 Oct 2013 01:36

thanks for your help guys.
I'll take the second one.
Maybe it's not nessesary to take the size as count.
The half size as count should work.
I'll try that...

btw: what do the "(" in "echo(!s!" ??
curiously there is no error in syntax but should be...!?

einstein1969
Expert
Posts: 947
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: center text with echo

#6 Post by einstein1969 » 18 Oct 2013 04:07

pumi wrote:thanks for your help guys.
I'll take the second one.
Maybe it's not nessesary to take the size as count.
The half size as count should work.
I'll try that...

btw: what do the "(" in "echo(!s!" ??
curiously there is no error in syntax but should be...!?


I have learned here, by jeb, to use this synthase that allows you to do the echo of empty variables. Otherwise it would give a message echo enabled / disabled.
It seems that the character "(" is the only one that is safe can be used anywhere without syntax errors.

"echo," instead of "echo." or "echo\"

PS. I have correct the previus code.
Einstein1969

pumi
Posts: 15
Joined: 05 Oct 2013 06:48
Location: Germany, BW

Re: center text with echo

#7 Post by pumi » 18 Oct 2013 14:05

thanks!

Post Reply