5

I'm implementing a batch program that execute some different things in different steps. For each step, I want to display a "friendly" text header like this:

*************************************************************
*                      MY FIRST STEP                        *
*************************************************************

"MY FIRST STEP" is a variable that could have a various length.

My question: do you have an algorithm or a function that could return this string as output with the string header to display in input parameter ?

Thanks in advance.

Regards

2 Answers 2

2

The width of all the symbols depends heavily on the font used. There is no easy way to measure pixel width of a string, but sometimes you don't need it.

Windows command line by default uses Lucida Console, a monospace font, which makes things easy. Example would be:

@echo off
setLocal EnableDelayedExpansion
set "STR=Boom^!"
set "SIZE=50"

set "LEN=0"
:strLen_Loop
   if not "!!STR:~%LEN%!!"=="" set /A "LEN+=1" & goto :strLen_Loop

set "stars=****************************************************************************************************"
set "spaces=                                                                                                    "

call echo %%stars:~0,%SIZE%%%
set /a "pref_len=%SIZE%-%LEN%-2"
set /a "pref_len/=2"
set /a "suf_len=%SIZE%-%LEN%-2-%pref_len%"
call echo *%%spaces:~0,%pref_len%%%%%STR%%%%spaces:~0,%suf_len%%%*
call echo %%stars:~0,%SIZE%%%

endLocal

SIZE here is the length of the block you want to output, make sure it's big enough to fit all the possible lines inside it.

I'll remind, that this will output a pretty block in monospace fonts only.

EDIT: Fixed the LEN initialization.

2
  • 1
    Just a remark IcedLance, when I use this program as a function in my batch file, it doesn't seem to work well. Do you know what could cause this problem of displaying ?
    – NicoFC34
    Sep 24, 2018 at 13:11
  • 1
    Found the solution. It was a problem of LEN reinit : set "LEN=0" at the beginning of the script. :-)
    – NicoFC34
    Sep 24, 2018 at 13:14
0

Here is a batch string that displays its parameters enclosed by asterisks :

@echo off
setlocal enabledelayedexpansion
rem Set the message to issue as second line
set "msg=*         %*         *"
rem Calculate the length of the string
set Length=0
for /l %%A in (1,1,1000) do if "%msg%"=="!msg:~0,%%A!" (
  set /a Length=%%A
  goto :doit
)
:doit
rem Create a string of asterisks of same length
set header=
for /l %%i in (1,1,%Length%) do set "header=!header!*
rem Issue the message
echo %header%
echo %msg%
echo %header%

Here is what it looks like when run :

image

3
  • Works great ! Thanks a lot. :-) Do you have a version with a fixed size of star square please ?
    – NicoFC34
    Sep 24, 2018 at 12:43
  • For fixed-size messages you can use snippets from the above to: (1) calculate length of parameter text, (2) calculate ((total-length - parameter-length) / 2) -1 to be the number of blanks, (3) make string with this number of blanks, (4) construct the message of "* blanks text blanks *", (5) create asterisks line as above, (6) Issue.
    – harrymc
    Sep 24, 2018 at 13:00
  • I prefer the answer of IcedLance below. Thanks a lot for your help. :-)
    – NicoFC34
    Sep 24, 2018 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .