Repeat a character in windows batch

This batch code will repeat a character a number of times.

It works in all modern versions of Windows.

:: CharRepeat
:: Repeats a character a specified number of times.
:: Arguments:
::  %1  ResultVar       The name of the environment variable that is populated
::                      with the result.
::  %2  Character       The character to repeat.
::  %3  Count           The number of times to repeat <character>.
:CharRepeat <resultvar> <character> <count>
SET "%~1="
FOR /L %%I IN (1,1,%~3) DO SET "%~1=!%~1!%~2"

You can use the code like so:

Call :CharRepeat RepeatedChar * 10
ECHO %RepeatedChar%
Call :CharRepeat RepeatedChar " " 10
ECHO %RepeatedChar%
Call :CharRepeat RepeatedChar _ 15
ECHO %RepeatedChar%

Hope this is useful.
Posted in Software and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.