请帮助我的脚本无效。
@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
)
我需要通过调用函数来打印特定字符x次,所以如果我这样做
call :printStrings retVal 3 #
预期输出
返回字符串:“###”
答案 0 :(得分:0)
您使用DelayedExpansion,但不是重要的一行
set "returnStr=%returnStr%%repeatChar%"
将失败,因为在行执行之前,%returnStr%和%repeatChar%的扩展已完成。
将其更改为set "returnStr=!returnStr!!repeatChar!"
,它应该可以正常工作
答案 1 :(得分:0)
::为什么使这么简单的事情复杂化
::************************************ ::********-Modify-******* set "Col=10" set "Char=#" ::********-Modify-******* :loop set /a loopEx+=1 set loop=%loop%%char% IF "%loopEx%" == "%col%" goto :Exloop goto :loop :Exloop ::********-Modify-******* Echo(%loop%>> File.txt ::********-Modify-******* ::****Code by S-S Guca Srbija 2019****
OR
::************************************
::********-Modify-*******
set "Col=10"
set "Char==&"
::********-Modify-*******
setlocal enabledelayedexpansion
:loop
set /a loopEx+=1
set loop=!loop!!char!
IF "!loopEx!" == "!col!" goto :Exloop
goto :loop
:Exloop
Set loop="!loop!"
setlocal disabledelayedexpansion
::********-Modify-*******
Echo(%loop%>> File.txt
::********-Modify-*******
::****Code by S-S Guca Srbija 2019****
OR
@echo off set "numbifor=15" set "numafter=15" set "mesag=i can do everything in MS-DOS" set "MyChar=*" ::*********************************************** Call :PrintString %numbifor% %MyChar% RetVal1 Set "numbifor=%RetVal1%" Call :PrintString %numafter% %MyChar% RetVal2 Set "numafter=%RetVal2%" goto:EXnext :PrintString set "Col=%~1" set "Char=%~2" :loop set /a "loopEx+=1" set loop=%loop%%char% IF "%loopEx%"=="%col%" goto:Exloop goto:loop :Exloop Set "%~3=%loop%" Set "loopEx=" Set "loop=" Goto:eof :EXnext ::*********************************************** set "msg=::%numbifor%-%mesag%-%numafter%" echo(%msg% | clip >nul Exit ::*********-Code by S-S Guca Srbija 2019-********
::根据需要修改代码