Please help with my script is not working.
@echo off
echo Printing repeated character
call :printStrings retVal 3 #
echo Returned String: "%retVal%"
PAUSE
:printStrings
(
setlocal EnableDelayedExpansion
set /a "Number=%~2"
rem set /a "counter=60-!Number!"
set "returnStr="
set "repeatChar=%~3"
rem echo Character to repeat: %repeatChar%
FOR /L %%G IN (1,1,!Number!) DO (
set "returnStr=%returnStr%%repeatChar%"
echo Adding character
)
)
(
endlocal
set "%~1=%returnStr%"
rem set "%~1=%repeatChar%"
exit /b
)
I need to print a specific character x number of times by calling the function, so if i do
call :printStrings retVal 3 #
Expected output will be
Returned String: “###”
You use the DelayedExpansion, but not at the important line.
set "returnStr=%returnStr%%repeatChar%"will fail, as the expansion of %returnStr% and also of %repeatChar% is done before the line will be executed.Change it to
set "returnStr=!returnStr!!repeatChar!"and it should work