2013-04-23 76 views
-1

好吧,這就是我所擁有的。批次爲循環陣列

@echo off 
setLocal EnableDelayedExpansion 
:begin 
set /a M=0 
set /a number=0 
set /p Input=You: 
echo %Input% >> UIS 
for /F "tokens=1 delims= " %%i in ("%Input%") do (
    set /a M+=1 
    set i!M!=%%i 
) 
del UIS 1>nul 2>nul 
:loop 
set /a number+=1 
set invar=!i%number%! 
echo %invar% 
pause > nul 
goto loop 

說,例如,輸入字符串爲「大聲笑,這是我的輸入字符串」 我想for循環設置我×!M!其中M = 1到「Lol」,其中M = 2 i!M!是「這個」並且其中M = 3我!M!是「是」等。當然,這不可能永遠持續下去,所以即使我不得不停下來,當M = 25或什麼的時候,並且說這串只有23字長。那麼當M = 24和25時,我!M!簡直是空的或未定義的。

任何幫助表示讚賞,謝謝。

+0

告訴我們什麼不工作有關腳本。 – 2013-04-23 15:40:45

+0

就像它只讀取字符串中的第一個單詞,並且不會將M的值設置爲1。 – 2013-04-23 15:55:45

回答

1

for /f逐行閱讀,而不是逐字閱讀。

這裏是在How to split a string in a Windows batch file?提出並修改您的具體情況的答案:

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

REM Set a string with an arbitrary number of substrings separated by semi colons 
set teststring=Lol this is my input string 
set M=0 

REM Do something with each substring 
:stringLOOP 
    REM Stop when the string is empty 
    if "!teststring!" EQU "" goto displayloop 

    for /f "delims= " %%a in ("!teststring!") do set substring=%%a 

    set /a M+=1 
    set i!M!=!substring! 

    REM Now strip off the leading substring 
    :striploop 
     set stripchar=!teststring:~0,1! 
     set teststring=!teststring:~1! 

     if "!teststring!" EQU "" goto stringloop 

     if "!stripchar!" NEQ " " goto striploop 

     goto stringloop 

:displayloop 
set /a number+=1 
set invar=!i%number%! 
echo %invar% 
pause > nul 
goto displayloop 

endlocal 
0

for /F命令劃分在一定的令牌的數量必須一次通過不同的替換參數進行處理的線路(%%我,%% j等)。 Plain for命令在中劃分一行undefined在迭代循環中逐個處理的單詞數量(用空格,逗號,分號或等號分隔)。這樣,你只需要改變此爲:通過這一個

for /F "tokens=1 delims= " %%i in ("%Input%") do (

for %%i in (%Input%) do (

PS - 我建議你寫在標準的形式排列,圍在方括號中的下標;更直觀這樣:

set i[!M!]=%%i 

set invar=!i[%number%]!