2013-04-23 46 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%]!