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
center text with echo
Moderator: DosItHelp
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: center text with echo
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.
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.
Re: center text with echo
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
-
- Expert
- Posts: 947
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: center text with echo
or more simple, need more test:
result:
EDIT: Correct for better centering and minor iteration (div 2)
Einstein1969
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.
Re: center text with echo
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'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...!?
-
- Expert
- Posts: 947
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: center text with echo
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
Re: center text with echo
thanks!