2012-03-05 76 views
0

請幫助我的腳本不工作。批處理文件:函數,創建特定字符的字符串,x次數

@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

_My腳本不會working_,啊哈,你在iPhone上進行測試,或者您試圖在Linux或哪一部分是不工作? – jeb 2012-03-05 09:22:49

+0

即使「添加字符」打印了3次,返回的值也始終爲空。 – Zenellie 2012-03-05 09:27:55

+0

http://rosettacode.org/wiki/Repeat_a_string#4DOS_Batch很有趣 – 2015-04-26 08:38:41

回答

0

您使用DelayedExpansion,但不在重要的行。
set "returnStr=%returnStr%%repeatChar%"將失敗,因爲%returnStr%和%repeatChar%的擴展完成之前該行將被執行。

將其更改爲set "returnStr=!returnStr!!repeatChar!",它應該工作

相關問題